Hi, This code scippets shows how to implement cusor in sql server.
/* Create two variables that would store the values returned by the fetch statement */
DECLARE @DepartmentName char(25) DECLARE @DepartmentHead char(25)
/* Define the cursor that can be used to access the records of the table,row by row */
DECLARE curDepartment cursor for SELECT vDepartmentName,vDepartmentHead from Department
-- Open the cursor
OPEN curDepartment
-- Fetch the rows into variables
FETCH curDepartment into @DepartmentName,@DepartmentHead
--Start a loop to display all the rows of the cursor
WHILE(@@fetch_status=0) BEGIN Print 'Department Name =' + @DepartmentName Print 'Department Head =' + @DepartmentHead
--Fetch the next row from the cursor FETCH curDepartment into @DepartmentName,@DepartmentHead END
-- Close the cursor
CLOSE curDepartment
-- Deallocate the cursor
DEALLOCATE curDepartment
Thanks and Regards S.S.Bajoria
|
No responses found. Be the first to respond and make money from revenue sharing program.
|