当获取状态= -1时,存储过程不会更新

时间:2014-10-20 06:36:17

标签: sql-server-2008 stored-procedures

我有一个sp,它将根据Table2中Table1的某些字段找到匹配项。如果找到匹配,则会将字段更新为“匹配”。如果没有,则会将该字段更新为空白。基本上它可以在有比赛时起作用,但如果没有,它就不会起作用。

IF (@@FETCH_STATUS = -1)
BEGIN
    Update Table1 Set Field2='' Where Field1 = @Field1;

    FETCH NEXT FROM Table1_cursor INTO @Field1
END
WHILE (@@FETCH_STATUS = 0)
BEGIN
    Update Table1 Set Field2='Match' Where Field1 = @Field1;

    FETCH NEXT FROM Table1_cursor INTO @Field1
END

1 个答案:

答案 0 :(得分:1)

使用set操作,连接table1和table2并使用case语句有条件地设置table1中的字段,这很可能更好。

update t1
set
    field2 = case when t2.field1 is not null then 'match' else '' end
from table1 t1
left join table2 t2
on t1.field1 = t2.field1