Rajeev
Rajeev

Reputation: 494

How to execute a procedure having cursor in SQL Server 2008?

I know how to execute a stored procedure in SQL Server2008 but I'm clueless in executing a stored procedure containing a cursor in it.

My code:

 BEGIN TRANSACTION 
 GO

DROP PROCEDURE SampleProcedure
GO

CREATE PROCEDURE SampleProcedure 
AS 
    DECLARE @FirstName varchar(64)

    DECLARE c1 CURSOR READ_ONLY
    FOR
        SELECT FIRST_NAME
        FROM EMPLOYEE

    OPEN c1

    FETCH NEXT FROM c1 INTO @FirstName

    WHILE @@FETCH_STATUS = 0
    BEGIN
        PRINT @FirstName

        FETCH NEXT FROM c1 INTO @FirstName
    END

    CLOSE c1
    DEALLOCATE c1

    COMMIT TRANSACTION 

I have seen executing a procedure with cursor in Oracle but couldn't find in SQL Server. Please tell me how to execute the stored procedure which contains cursor.

---Thanks

Upvotes: 0

Views: 1914

Answers (1)

PowerStar
PowerStar

Reputation: 895

Executing the SP.

EXEC SampleProcedure

Upvotes: 1

Related Questions