--游标
-- 申明游标
declare cursor_001 cursor for select DICT_CODE,DICT_DESCRIBE,DICT_VALUE from @variable_Table
-- 开启游标
open cursor_001
fetch next from cursor_001 into @DICT_CODE,@DICT_DESCRIBE,@DICT_VALUE
while (@@fetch_status = 0)
begin
if exists(select top 1 OBJECT_ID from E3_MEASURE_U2000V1R7C00 where OBJECT_ID = @TDTNAME and DICT_CODE = @DICT_CODE and TDT = @STAND_TDT)
begin
update E3_MEASURE_U2000V1R7C00 set VALUE = @DICT_VALUE where OBJECT_ID = @TDTNAME and DICT_CODE = @DICT_CODE and TDT = @STAND_TDT
end
else
begin
insert into E3_MEASURE_U2000V1R7C00 values (@TDTNAME,''TDT'',@DICT_CODE,@DICT_DESCRIBE,@DICT_VALUE,@STAND_TDT)
end
fetch next from cursor_001 into @DICT_CODE,@DICT_DESCRIBE,@DICT_VALUE
end
close cursor_001
deallocate cursor_001
set @count=@count-1
end
'
print @RunSQL
EXECUTE SP_EXECUTESQL @RunSQL
本文介绍了一种使用游标进行数据库记录逐条读取的方法,并在此基础上实现对另一表的数据更新或插入操作。通过示例代码展示了如何声明、打开、读取及关闭游标,并在循环中判断条件执行更新或插入。
393

被折叠的 条评论
为什么被折叠?



