Click here to Skip to main content
11,437,397 members (35,178 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 21:53pm
Edited 6-Oct-11 21: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)



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | 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