其他分享
首页 > 其他分享> > 游标循环时跳转

游标循环时跳转

作者:互联网

DECLARE @Name NVARCHAR(50),@NRIC NVARCHAR(15),@PersonID INT
DECLARE CUR_Test1 CURSOR FOR
SELECT [Name],NRIC,PersonID FROM tblCtcPersonTest1
OPEN CUR_Test1
FETCH NEXT FROM CUR_Test1 INTO @Name,@NRIC,@PersonID;
 WHILE @@FETCH_STATUS =0
    BEGIN

        IF    EXISTS(SELECT * FROM tblCtcPersonTest WHERE NRIC=@NRIC )
        BEGIN
              GOTO UPDATE_IMPORT_RESULT;
        END

        UPDATE tblCtcPersonTest SET Flag='not Go To' WHERE PersonID=@PersonID

        IF CHARINDEX('*',@NRIC)>0
        BEGIN
            INSERT INTO tblCtcPersonTest([Name],[PersonID],NRIC)VALUES(@Name,@PersonID,@NRIC)
        END
        ELSE
        BEGIN
             UPDATE tblCtcPersonTest1 SET Remarks='cant insert' WHERE PersonID=@PersonID
            
        END
        UPDATE_IMPORT_RESULT:
        UPDATE tblCtcPersonTest SET Remarks='Go To' WHERE PersonID=@PersonID
    FORNEXT:
    FETCH NEXT FROM CUR_Test1 INTO @Name,@NRIC,@PersonID;
    END
CLOSE CUR_Test1;
DEALLOCATE CUR_Test1

标签:Test1,NRIC,CUR,PersonID,游标,UPDATE,循环,跳转,Name
来源: https://www.cnblogs.com/TieJing/p/11434641.html