SQL语句where子句后面like表达式怎么写

有table1:id varchar(50),总数 int
table2:id varchar(50),数量 int
用游标遍历:
declare @id varchar(50),@total
declare mycur cursor for
select id from table1
open mycur
fetech next from mycur into @id
while @@fetch_status=0
begin
set @total=sum(数量) from table2 where id like @id+'%'
update table1 set 总数=@total where id=@id
fetch next from mycur into @id
end
close mycur
问题是like后面不是应该都在单撇号里吗?id的记录类似001、001001、001002...,总数就是他所有子单位的总和,整个表达式还有哪里不对的?

1、你要这么写,like是肯定用不了了,用截取吧
2、你下面跟的update也有问题,你要不也用截取,不过用截取的话你update的会事多条语句
declare @id varchar(50),@total
declare mycur cursor for
select id from table1
open mycur
fetech next from mycur into @id
while @@fetch_status=0
begin
set @total=sum(数量) from table2 where substring(id,1,len(@id) =@id
update table1 set 总数=@total where id=@id
fetch next from mycur into @id
end
close mycur
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-12-13
id的记录类似001、001001、001002...,那么id应该是string类型
那么set @total=sum(数量) from table2 where id like @id+'%'
应该改为: set @total=sum(数量) from table2 where id like ‘@id+’%‘’
第2个回答  2010-12-13
set @total=sum(数量) from table2 where id like ‘“’+@id+'%”'

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网