Code:
/* Create a table */ Create table Department (vDepartmentName char(25),vDepartmentHead char(25)) /* 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
Comment