Click here to Skip to main content
11,701,855 members (62,747 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server
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 6-Oct-11 20:53pm
Edited 6-Oct-11 20:56pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Simon_Whale at 7-Oct-11 4:34am
   
Nice links +5
CodeHelper_PS at 7-Oct-11 4:37am
   
Cheers!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 :-

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
  Permalink  
v4

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

  Print Answers RSS
0 OriginalGriff 314
1 Sergey Alexandrovich Kryukov 310
2 Maciej Los 280
3 Mika Wendelius 119
4 So_Keeee 100
0 OriginalGriff 8,324
1 Sergey Alexandrovich Kryukov 7,422
2 CPallini 4,912
3 Maciej Los 4,196
4 Mika Wendelius 3,556


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 7 Oct 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100