SQL Cursor:-
SQL Cursor is used to select some value from table and save the table to cursor and process the values using while and fetch.
Example Store Procedure:-
DECLARE
@ID int ,
@EmployeeID int ,
@CompanyID int ,
@JobTitleID int ,
@CategoryID int ,
@GradeID int ,
@ReportTo int;
DECLARE cursor_name CURSOR FOR
SELECT ID ,
EmpID AS EmployeeID ,
CompanyId ,
JobTitleId ,
CategoryId ,
GradeId ,
ReportTo where account_status=1
OPEN cursor_name;
FETCH next FROM cursor_name INTO @ID , @EmployeeID , @CompanyID , @JobTitleID , @CategoryID , @GradeID , @ReportTo;
WHILE @@FETCH_STATUS = 0
BEGIN
print @EmployeeID;
FETCH next FROM cursor_name INTO @ID , @EmployeeID , @CompanyID , @JobTitleID , @CategoryID , @GradeID , @ReportTo;
end
CLOSE cursor_name;
DEALLOCATE cursor_name;
Advertisement
Screen shots:-
SQL Cursor is used to select some value from table and save the table to cursor and process the values using while and fetch.
Example Store Procedure:-
DECLARE
@ID int ,
@EmployeeID int ,
@CompanyID int ,
@JobTitleID int ,
@CategoryID int ,
@GradeID int ,
@ReportTo int;
DECLARE cursor_name CURSOR FOR
SELECT ID ,
EmpID AS EmployeeID ,
CompanyId ,
JobTitleId ,
CategoryId ,
GradeId ,
ReportTo where account_status=1
OPEN cursor_name;
FETCH next FROM cursor_name INTO @ID , @EmployeeID , @CompanyID , @JobTitleID , @CategoryID , @GradeID , @ReportTo;
WHILE @@FETCH_STATUS = 0
BEGIN
print @EmployeeID;
FETCH next FROM cursor_name INTO @ID , @EmployeeID , @CompanyID , @JobTitleID , @CategoryID , @GradeID , @ReportTo;
end
CLOSE cursor_name;
DEALLOCATE cursor_name;
Advertisement
Screen shots:-
Thanks
ReplyDelete