SQLserver数据库篇
之后马上想到要用游标操作,操作思想如下:
DECLARE @aaa varchar(10)
SET @aaa = 'nihao'
select @aaa = tab.name from SysObjects as tab where 1=2
PRINT @aaa
-- 预期结果为: (空字符)
-- 输出结果为:nihao
-- 没想到啊,就这个小东西让我找了半天!
DECLARE @bolid varchar(30), @bolcode varchar(30), @row int
-- 声明游标
DECLARE string_find_cursor CURSOR FAST_FORWARD
FOR select_statement;
-- 打开游标
OPEN string_find_cursor;
-- 获取游标的值,分别赋给@bolcode,@bolid,@row
FETCH NEXT FROM string_find_cursor
INTO @bolcode,@bolid,@row;
-- 获取游标状态,如果为0则继续执行,否则结束while循环
WHILE @@FETCH_STATUS = 0
BEGIN
/* 执行要实现某些功能的语句 */
FETCH NEXT FROM string_find_cursor
INTO @bolcode,@bolid,@row;
END;
-- 关闭游标
CLOSE string_find_cursor;
-- 释放游标的内存
DEALLOCATE string_find_cursor;
相关文章