
SQL语句where子句后面like表达式怎么写
有table1:idvarchar(50),总数inttable2:idvarchar(50),数量int用游标遍历:declare@idvarchar(50),@tot...
有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...,总数就是他所有子单位的总和,整个表达式还有哪里不对的? 展开
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...,总数就是他所有子单位的总和,整个表达式还有哪里不对的? 展开
3个回答
展开全部
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
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
展开全部
id的记录类似001、001001、001002...,那么id应该是string类型
那么set @total=sum(数量) from table2 where id like @id+'%'
应该改为: set @total=sum(数量) from table2 where id like ‘@id+’%‘’
那么set @total=sum(数量) from table2 where id like @id+'%'
应该改为: set @total=sum(数量) from table2 where id like ‘@id+’%‘’
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
set @total=sum(数量) from table2 where id like ‘“’+@id+'%”'
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询