Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stored procedure LANGUAGE 'sql'

Status
Not open for further replies.

djam

Technical User
Joined
Nov 15, 2002
Messages
223
Location
CA
Does sql stored procedures support loops??

thanks " ahhh computers, how they made our lives much simpler ;) "
 
Hi djam

Yes sql stored procedures do support loops. You can use a WHILE statement although SQL uses it slightly differently to say VB code. You can also use a CURSOR which is a SQL recordset, which you loop through and execute other commands per record.

CURSORS should be avoid as they can slow your query times down. Often you can get by without using cursors by using mutliple sql statments. The flip side of the coin is that often there are no alternatives to using a cursor but if the cursor is written correctly and optimally then they can be a powerful function within your procedure.

Have a look in SQL BOL index for the WHILE and CURSOR keyword. You can also search using the keyword search in this forum, there are hundreds of articles on procedures, cursors, loops and while statements.

Hope this helps

John
 
You can loop like this:

declare @i
set @i=1

while @i<100
begin
set @i=@i+1
end ________________________________________________________________________________
If you do not like change, get out of the IT business...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top