这个存储不能用?急呀!!!!

uestc_yrq 2002-12-06 03:23:43
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 cllb=@myclmc and clmc=@myclmc and ggxh=@thggxh and gydw=@thgydw and 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

当我输入条件时发现update总是不能执行到!,
谁解决了我还有100分奉送,
http://expert.csdn.net/Expert/topic/1232/1232364.xml?temp=.4891779
...全文
28 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
uestc_yrq 2002-12-09
  • 打赏
  • 举报
回复
谢谢各位的答复,去掉空格?不成功!
主要是这样的,如果cllb='电子材料';clmc='集成电路',ggxh='74L301',
gydw=Null,jldw=Null,update='2002-11-11',je=2时
在这些条件下,第一次执行insert,这是对得,数据库中没有这条记录,可是
当我将上面的条件只改je=20时,存储报错说:不能插入相同的记录?
uestc_yrq 2002-12-09
  • 打赏
  • 举报
回复
谢谢各位的关心,大家为我忙了一阵,再次感谢
uestc_yrq 2002-12-09
  • 打赏
  • 举报
回复
插入后,gydw与jldw字段都是'',不再是null,我想也不应该是Null因为数据库设计他们不能为空。
接受了zxdragon(zxdragon)的答案,分分,请zxdragon(zxdragon)到
http://expert.csdn.net/Expert/topic/1232/1232364.xml?temp=.5257379登记一下,100分全归你了。
学到一点,存储的参数最好有个默认值
qmigh 2002-12-09
  • 打赏
  • 举报
回复
第一次执行insert,这是对得,数据库中没有这条记录

这时候你查看一下数据库,看看gydw、jldw字段是null还是''
zxdragon 2002-12-09
  • 打赏
  • 举报
回复
检索没有对参数输入NULL值作判断。
将检索条件改为
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函数了。
qmigh 2002-12-08
  • 打赏
  • 举报
回复
请你比较详细地说明:
什么情况下update语句能够执行到;
什么情况下update语句无法执行到。
这样才比较好找出原因。

pengdali(大力),如果楼主的数据库使用默认配置,有没有rtrim()应该都是一样的。
CoolAbu 2002-12-08
  • 打赏
  • 举报
回复
Nod...

Studying...
pengdali 2002-12-08
  • 打赏
  • 举报
回复
对呀不要exists也行呀!
j9988 2002-12-08
  • 打赏
  • 举报
回复
就是说你已经把源文件导入一个表中了,应该两句可完成,大体是这样,你调整一下:
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)
pengdali 2002-12-08
  • 打赏
  • 举报
回复
写错!
rtrim不好意思!
pengdali 2002-12-08
  • 打赏
  • 举报
回复
用rtime去掉字段后面的空格:
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
uestc_yrq 2002-12-08
  • 打赏
  • 举报
回复
数据库是其他人设计的,我没有办法更改,数据库的设计人员设计的主键就是
where从句的5个字段。我也相信是这个从句的问题,因为如果我输入的5个不为空的变量的话,update从句可以执行到,现在就是修改这个判断的问题
ben988211 2002-12-08
  • 打赏
  • 举报
回复
EXISTS(select * from clsj where cllb=@myclmc and clmc=@myclmc and ggxh=@thggxh and gydw=@thgydw and jldw=@thjldw)

你这句话是不是想判断出记录是否存在,你的where 条件为什么构造的这么复杂,你的表没有主键吗?我认为问题出在where 条件上。
bear7788 2002-12-06
  • 打赏
  • 举报
回复
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
Sunhawk 2002-12-06
  • 打赏
  • 举报
回复
能不能写详细点?

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧