有兩張表 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那。