麻烦各位高手给找一下问题所在哈.
两个表,表一:ts 表二tsg_tsgc 两个表都有相同的字段SJHRK(char(12)),需要根据
tsg_tsgc表中的title更新ts中的 "正题名 "
存储过程如下:
REATE PROCEDURE ts_gx AS
declare @sjhrk char(12) , @sjhrka varchar (12) , @lr varchar (200)
declare ts_gx cursor
for
select SJHRK from ts
open ts_gx
fetch next from ts_gx into @sjhrk
while @@fetch_status=0
begin
select @sjhrk=sjhrk from tsg_tsgc where sjhrk=@sjhrk
select @lr=title from tsg_tsgc where sjhrk=@sjhrk
select @sjhrka=sjhrk from ts where sjhrk=@sjhrk
if @sjhrk <> @sjhrka
begin
update ts set 正标题=@lr where sjhrk=@sjhrka
end
fetch next from ts_gx into @sjhrk
end
close ts_gx
deallocate ts_gx
GO
现在问题是:只能更一部分数据,有一部份无法更,请各位高人指点一下.
------解决方案--------------------好像不需要使用游标吧?
只是update 的话看这个:
update a set 正标题 = title
from ts a
join ts_gx b on a.sjhrk=b.sjhrk
-------
不知是不是你要表示的意思:
即根据ts_gx表的title 列更新 ts 表的 正标题 列
------解决方案--------------------update a set 正标题=b.title from ts a,tsg_tsgc b where a.sjhrk=b.sjhrk
------解决方案--------------------ts_gx這個表不能掉吧。
Update
ts
set
正标题 = tsg_tsgc.title
From
ts
Inner Join
tsg_tsgc
On ts.sjhrk = tsg_tsgc.sjhrk
Inner Join
ts_gx
On ts_gx.sjhrk = tsg_tsgc.sjhrk
Or
Update
ts
set
正标题 = tsg_tsgc.title
From
ts, tsg_tsgc, ts_gx
Where ts.sjhrk = tsg_tsgc.sjhrk
And ts_gx.sjhrk = tsg_tsgc.sjhrk
------解决方案--------------------update a set 正标题=b.title from ts a,tsg_tsgc b where a.sjhrk=b.sjhrk