SQL Cursor


DECLARE @FIRST_NAME nvarchar(256) 
DECLARE @LAST_NAME nvarchar(256) 

SET NOCOUNT ON

DECLARE csr CURSOR
FOR
SELECT TOP 10
FIRST_NAME,
LAST_NAME
FROM Customer
WHERE
LAST_NAME LIKE 'C%'
AND FIRST_NAME IS NOT NULL
FOR UPDATE

OPEN csr

FETCH NEXT
FROM csr
INTO 
@FIRST_NAME, 
@LAST_NAME

WHILE (@@FETCH_STATUS = 0)
BEGIN

	BEGIN TRY
	   PRINT @LAST_NAME + ', ' + @FIRST_NAME
	END TRY
	BEGIN CATCH
		SELECT ERROR_NUMBER() AS ErrorNumber
		 ,ERROR_SEVERITY() AS ErrorSeverity
		 ,ERROR_STATE() AS ErrorState
		 ,ERROR_PROCEDURE() AS ErrorProcedure
		 ,ERROR_LINE() AS ErrorLine
		 ,ERROR_MESSAGE() AS ErrorMessage;

	     GOTO LABEL_EXIT
	END CATCH;

	FETCH NEXT
	FROM csr
	INTO 
	@FIRST_NAME, 
	@LAST_NAME
END

LABEL_EXIT:
CLOSE csr

DEALLOCATE csr

SET NOCOUNT OFF	

MSDN: CURSOR