有两张表 ms ,xhl_doi_2015 , 其中 ms 表中的 字段 fenleihao 包含 xhl_doi_2015 表中的pacs ,我想实现的结果是 统计 ms表中 xhl_doi_2015表中各个pacs出现的次数,并将该次数 写到 xhl_doi_2015 的 tougao 字段中。
其中xhl_doi_2015 表结构如下:
sql1,
declare @id int
declare @maxid int
declare @minid int
declare @count int
select @maxid= max(a.id) from xhl_doi_2015 a where tougao is null
select @minid= min(a.id) from xhl_doi_2015 a where tougao is null
if ((@maxid is not null) and (@minid is not null))
begin
while (@maxid>=@minid)
begin
select @count=count(a.id)
from ms b ,xhl_doi_2015 a
where b.pacs like '%'+a.pacs+'%' and a.id=@minid and a.tougao is null
update xhl_doi_2015 set tougao=@count where id=@minid and tougao is null
select @minid= min(a.id) from xhl_doi_2015 a where tougao is null
end
end
go
sql2
declare @id int
declare @maxid int
declare @minid int
declare @count int
declare @pacs int
select @maxid= max(a.id) from xhl_doi_2015 a where tougao is null
select @minid= min(a.id) from xhl_doi_2015 a where tougao is null
while (@maxid>=@minid)
begin
select @pacs=min(pacs) from xhl_doi_2015 where id=@minid and tougao is null
select @count =count(id) from ms where fenleihao like '%'+@pacs+'%'
update xhl_doi_2015 set tougao=@count where id=@minid and tougao is null
select @minid= min(a.id) from xhl_doi_2015 a where tougao is null
end
go
实际测试中,游标1将实际数据写到了xhl_doi_2015 的tougao字段中,但游标2 执行后,xhl_doi_2015 表的投稿字段,全都被 update 为 0 。
但我觉的 游标2的 逻辑应该也没错,但不知道为什么 结果都是0那。