Click here to Skip to main content
15,881,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Sir

Please Explained About Cursors in Sql Server2005 with Example

and Use Of the Cursors

I need to Detail Expalanation of Cursors

Can we use Cursors with out create Procedure

Plz Help me very imp to me

Thanks And Regards

JAYA
Posted
Updated 6-Oct-11 20:56pm
v2

 
Share this answer
 
Comments
Simon_Whale 7-Oct-11 4:34am    
Nice links +5
P.Salini 7-Oct-11 4:37am    
Cheers!
Codehelper, has given great links that you should read also

Firstly the thing that you need to be aware of is that cursors can be an expensive operation and should be avoided where possible.

An example Cursor is as follows

SQL
DECLARE @TableName VARCHAR(2000)

DECLARE db CURSOR FOR
SELECT name
FROM sysobject
WHERE xtype = 'u'

OPEN DB
FETCH NEXT FROM db INTO @TableName

WHILE @@FETCH_STATUS = 0
BEGIN
 PRINT @TableName
 FETCH NEXT FROM db INTO @TableName
END

CLOSE db
DEALLOCATE db


Can we use Cursors with out create Procedure - YES
 
Share this answer
 
1.For Defination Purpose :- http://sqlserverpedia.com/wiki/Built-in_Functions_-_Cursor_Functions[^]

2.Cursor is Basically Used for PL/SQL Even We Can Also Used Cursor in Stored Procedure OR Without Stored procedure it's Depands on Condition.
Synax and Example of Cursor :-

SQL
Declare @row int
Declare @CODE varchar(10) 
Declare @VN varchar(50) 
Select @row = acode from  Tblmst_Serial
DECLARE CU CURSOR FOR SELECT TYP,VNAME FROM Tblmst_Serial
  OPEN CU
    while(@row >0)  // alternative we can Also Use here "While(@@fetch_status<>0)"
		begin
		fetch next from CU into @CODE,@VN
		print @CODE
		print @VN
		 Set @row =@row -1
		end
   Close CU
 Deallocate CU
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900