In this post i will show how to use a cursor in SQL. Cursor works on a row by row basis.
In the below example for cursor i will print the name of the employees from the employee table using cursor.
DECLARE cur CURSOR
FOR
SELECT empname
FROM empDataDetails
DECLARE @name VARCHAR(1000)
OPEN cur
FETCH NEXT
FROM cur
INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @name
FETCH NEXT
FROM cur
INTO @name
END
CLOSE cur
DEALLOCATE cur
In the below example for cursor i will print the name of the employees from the employee table using cursor.
DECLARE cur CURSOR
FOR
SELECT empname
FROM empDataDetails
DECLARE @name VARCHAR(1000)
OPEN cur
FETCH NEXT
FROM cur
INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @name
FETCH NEXT
FROM cur
INTO @name
END
CLOSE cur
DEALLOCATE cur
No comments:
Post a Comment