SQL Server 游标写入数据

https://www.cnblogs.com/wanghonghu/p/4093411.html

 

set identity_insert 表名 ON --允许对自增列Id插入指定数据
insert into table_name(Id,Name) values(1,'test')
set identity_insert 表名 OFF --关闭对自增列Id插入指定数据


https://www.cnblogs.com/mcgrady/p/4182486.html

 

-- 方法1:游标
-- 声明变量
DECLARE
    @empid AS INT,
    @firstname AS NVARCHAR(10),
    @lastname AS NVARCHAR(20);
    
-- 声明游标
DECLARE C_Employees CURSOR FAST_FORWARD FOR
    SELECT empid,firstname,lastname
    FROM HR.Employees
    ORDER BY empid;
    
OPEN C_Employees;

-- 取第一条记录
FETCH NEXT FROM C_Employees INTO @empid,@firstname,@lastname;

WHILE @@FETCH_STATUS=0
BEGIN
    -- 操作
    UPDATE HR.Employees SET fullname= @firstname+' '+@lastname WHERE empid=@empid;
    
    -- 取下一条记录
    FETCH NEXT FROM C_Employees INTO @empid,@firstname,@lastname;
END

-- 关闭游标
CLOSE C_Employees;

-- 释放游标
DEALLOCATE C_Employees;

posted @ 2021-09-28 18:16  LuoCore  阅读(177)  评论(0编辑  收藏  举报