SQLSERVER 遍历循环的两种方式很详细有源码(2)

2.游标循环

SQLSERVER 遍历循环的两种方式很详细有源码(2)_第1张图片

Create table WS_Student
(
      [Id] int primary key not null,
    [My_Cocode] [int],
    [My_SCocode] [int],
    [userId] [bigint],
    [SetCName] [varchar](50) NULL,
    [SetEName] [varchar](50) NULL,
    [SetPcode] [varchar](50) NULL,
    [SetScode] [varchar](50) NULL,
    [SetValue] [varchar](50) NULL,
    [SetSql] [varchar](500) NULL,
    [Sort] [Int] NULL,
    [IsTurnOn] [bit] NULL,
    [IsSystem] [bit] NULL,
    [Remarks] [varchar](50) NULL,
    [CreatedUserId] [bigint] NULL,
    [CreatedUserName] [varchar](50) NULL,
    [CreatedTime] [datetime] NULL,
    [UpdatedUserId] [bigint] NULL,
    [UpdatedUserName] [varchar](50) NULL,
    [UpdatedTime] [datetime] NULL,
    [IsDeleted] [bit] NULL
)
INSERT INTO WS_Student VALUES(1,21114001,21114005,0,'是否学生','是否学生','WS_Student',
'WS_Student_IsStudent','1',null,1,1,1,'是否学生',999999,'shil',GETDATE(),999999,'shil',GETDATE(),0)

INSERT INTO WS_Student VALUES(2,21114001,21114005,0,'是否学生2','是否学生2','WS_Student',
'WS_Student_IsStudent2','1',null,1,1,1,'是否学生2',999999,'shil',GETDATE(),999999,'shil',GETDATE(),0)

这里的 WS_Student2 按照上面方法再创建一个即可

--循环遍历修改记录--
DECLARE @i int;
DECLARE @Name nvarchar(50);
DECLARE @id int;
SET @i=1;
declare WS_Student_cursor cursor
for(SELECT userId,SetCName from  WS_Student2)
open WS_Student_cursor
fetch next from WS_Student_cursor into @id ,@Name
while @@FETCH_STATUS=0
BEGIN 
    
INSERT INTO WS_Student VALUES((userId,21114001,21114005,0,'是否学生2','是否学生2','WS_Student',
'WS_Student_IsStudent2','1',null,1,1,1,'是否学生2',999999,'shil',GETDATE(),999999,'shil',GETDATE(),0)
    fetch next from WS_Student_cursor into @id ,@Name  
END
close WS_Student_cursor
deallocate WS_Student_cursor

你可能感兴趣的:(sqlserver,sql,数据库)