set @thggxh=isnull(@myggxh,"")
set @thgydw=isnull(@mygydw,"")
set @thjldw=isnull(@myjldw,"")
改为
set @thggxh=isnull(@myggxh,'')
set @thgydw=isnull(@mygydw,'')
set @thjldw=isnull(@myjldw,'')
别的都行,也能update
set @thgydw=isnull(@mygydw,"")
set @thjldw=isnull(@myjldw,"")
改为
set @thggxh=isnull(@myggxh,'')
set @thgydw=isnull(@mygydw,'')
set @thjldw=isnull(@myjldw,'')
别的都行,也能update
where从句的5个字段。我也相信是这个从句的问题,因为如果我输入的5个不为空的变量的话,update从句可以执行到,现在就是修改这个判断的问题
CREATE PROCEDURE doupcl
@mycllb varchar(50),
@myclmc varchar(50),
@myggxh varchar(50),
@mygydw varchar(50),
@myjldw varchar(50),
@myupdate datetime,
@myje money
AS
declare @mycount int
declare @SqlString nvarchar(1000)
declare @thggxh varchar(50)
declare @thgydw varchar(50)
declare @thjldw varchar(50)
set @thggxh=isnull(@myggxh,"")
set @thgydw=isnull(@mygydw,"")
set @thjldw=isnull(@myjldw,"")
set @mycount=0
if EXISTS(select * from clsj where rtime(cllb)=@myclmc and rtime(clmc)=@myclmc and rtime(ggxh)=@thggxh and rtime(gydw)=@thgydw and rtime(jldw)=@thjldw) begin
update clsj
set je = @myje,upyeardate=@myupdate
where cllb=@myclmc and clmc=@myclmc and ggxh=@thggxh and gydw=@thgydw and jldw=@thjldw
end
else begin
insert into clsj(cllb,clmc,ggxh,gydw,jldw,je,upyeardate)
values(@mycllb,@myclmc,@thggxh,@thgydw,@thjldw,@myje,@myupdate)
end
GO
rtrim不好意思!
update A
set je = B.je,upyeardate=@myupdate
from clsj A,源表 B
where A.cllb=B.cllb and A.clmc=B.clmc and A.ggxh=B.thggxh and A.gydw=B.thgydw and A.jldw=B.thjldw insert into clsj(cllb,clmc,ggxh,gydw,jldw,je,upyeardate)
select cllb,clmc,ggxh,gydw,jldw,je,@myupdate from 源表 B
where not exists (select 1 from clsj where cllb=B.cllb and clmc=B.clmc and ggxh=B.thggxh and gydw=B.thgydw and jldw=B.thjldw)
什么情况下update语句能够执行到;
什么情况下update语句无法执行到。
这样才比较好找出原因。pengdali(大力),如果楼主的数据库使用默认配置,有没有rtrim()应该都是一样的。
主要是这样的,如果cllb='电子材料';clmc='集成电路',ggxh='74L301',
gydw=Null,jldw=Null,update='2002-11-11',je=2时
在这些条件下,第一次执行insert,这是对得,数据库中没有这条记录,可是
当我将上面的条件只改je=20时,存储报错说:不能插入相同的记录?
将检索条件改为
EXISTS(select * from clsj where ((cllb=@mycllbor ISNULL(@mycllb,'')='') and (clmc=@myclmc or ISNULL(@myclmc,'')='') and (ggxh=@thggxh or ISNULL(@thggxh ,'')='') and (gydw=@thgydw or ISNULL(@thgydw ,'')='') and (jldw=@thjldw or ISNULL(@thjldw ,'')=''))
如果不产生歧义,你可以将输入参数默认值设为''
这样检索条件中就可以不使用ISNULL函数了。
接受了zxdragon(zxdragon)的答案,分分,请zxdragon(zxdragon)到
http://expert.csdn.net/Expert/topic/1232/1232364.xml?temp=.5257379登记一下,100分全归你了。
学到一点,存储的参数最好有个默认值