学习总结之三(SQL SERVER游标CURSOR的使用)
SET LOCK_TIMEOUT 1800
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN TRANSACTION
DECLARE @PRD_NO VARCHAR(30)
DECLARE @CUS_NO VARCHAR(20)
DECLARE @STD_LEVEL VARCHAR(10)
DECLARE @USR VARCHAR(12)
DECLARE MY_CURSOR CURSOR FOR
SELECT DISTINCT PRD_NO,CUS_NO,STD_LEVEL,USR FROM CUS_ML_GGEC HOLDLOCK WHERE PRD_NO+'*'+CUS_NO NOT IN (SELECT PRD_NO+'*'+CUS_NO FROM PRDT_AUTH )
OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @PRD_NO,@CUS_NO,@STD_LEVEL,@USR
WHILE (@@FETCH_STATUS = 0)
BEGIN
INSERT INTO PRDT_AUTH (PRD_NO,CUS_NO,ZS_NO,STD_LEVEL,USR,SYS_DATE,CLS_ID)
VALUES( @PRD_NO,@CUS_NO,@PRD_NO+@CUS_NO,@STD_LEVEL,@USR,GETDATE(),'T' )
FETCH NEXT FROM MY_CURSOR INTO @PRD_NO,@CUS_NO,@STD_LEVEL,@USR
END
IF @@ERROR = 0
COMMIT TRANSACTION
ELSE
ROLLBACK
或者:
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR
SET TRANSACTION ISOLATION LEVEL READ COMMITTED