Connecting Tech Pros Worldwide Help | Site Map

While loop with dynamic sql

Newbie
 
Join Date: Apr 2009
Posts: 28
#1: May 8 '09
Hi could you please help me with the following...
The syntax for while loop is

While Exists(select * from Customers)
End While

How can I use the above select statment using dynamic SQL?
Member
 
Join Date: Aug 2008
Posts: 49
#2: May 8 '09

re: While loop with dynamic sql


That syntax won't work and that really is not the way to go anyway.

Try setting up a cursor (Google sql cursor for details). That will keep looping through the recordset until it is complete.
Newbie
 
Join Date: Apr 2009
Posts: 28
#3: May 9 '09

re: While loop with dynamic sql


The above code is just a part of my SP.In that SP I m already using a cursor and in ttht cursor I m using while loop...Bcoz cursors have the tendency to make system slow I think so it wont be a good prog. approach to use it..so could u plz help me
iburyak's Avatar
Expert
 
Join Date: Nov 2006
Posts: 1,017
#4: May 11 '09

re: While loop with dynamic sql


Try this example:

Expand|Select|Wrap|Line Numbers
  1. declare @a table (ID int, Val varchar(10))
  2. insert into @a values(1, 'aaa')
  3. insert into @a values(2, 'bbb')
  4. insert into @a values(3, 'ccc')
  5. insert into @a values(4, 'ddd')
  6.  
  7. Declare @ID int 
  8. Select @ID = min(ID) from @a
  9.  
  10. While Exists(select * from @a where ID >= @ID)
  11. BEGIN
  12.     Select * from @a where ID = @ID
  13.     Select @ID = min(ID) from @a where ID > @ID
  14. END 
Let me know if you have more specific question.
Good Luck.
Newbie
 
Join Date: Apr 2009
Posts: 28
#5: May 13 '09

re: While loop with dynamic sql


Thnks for ur reply ..I want to know how can I use the following statement inside while loop
'Select * from '+@localDBName+'..Customers'
Newbie
 
Join Date: Jul 2009
Posts: 9
#6: Jul 16 '09

re: While loop with dynamic sql


That means you want to use dynamic sql

DECLARE @Sql nvarchar(100)
SET @Sql ='Select * from '+ @localDBName + '..Customers'
EXEC @Sql

Hope this will work for the part of you SP

-HAPPY QUERY

-Pankaj Tambe
Delerna's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 783
#7: Jul 17 '09

re: While loop with dynamic sql


Quote:

Originally Posted by SnehaAgrawal View Post

Bcoz cursors have the tendency to make system slow I think so it wont be a good prog. approach to use it..

Absolutely correct
A while loop and the use of variables to adjust the results of a query
based on the values of the variables definiteley has superior performance than using a cursor.

Never resort to cursors unless you have no other way to achive your goal
and performance is not an issue or the recordset is small.


One correct use for cursors (well correct until I find a better way)
You have a table that lists a set of email addresses that you select as a result of some event.
You loop through the recordset using a cursor so that you can
xp_sendmail to each address.
In this scenario each email sent is similar but different from each other
(not the same)


DYNAMIC QUERIES
These are great and I often use them as a way of not using cursors.
One word of caution however. Don't pass parts of the dynamic query string in as parameters. Doing so opens the door for hackers to use SQL injection.


Just my 2 cents worth
Newbie
 
Join Date: Apr 2009
Posts: 28
#8: Jul 29 '09

re: While loop with dynamic sql


Hi I want to know how can I use a Dynamic SQL Statement inside a While loop

I mean In cursors we fetch rows one by one how can I do that in a while loop that too using Dynamic SQL bcoz I have to access a different server.No issues regarding SQL Injection bcoz this is done internally user isn't invloved.
Delerna's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 783
#9: Jul 30 '09

re: While loop with dynamic sql


You haven't given much of a description of what you are trying to achieve.
Why would you want to select records one by one. You may as well use a cursor if you must do that.
The whole point is that working through a recordset 1 record at a time is slow in comparison to working on the whole recordset as a batch
In other words using a dynamic query to work through a recordset 1 record at a time is no more efficient than using a cursor. Probably less efficient.


The basic idea is like this
Expand|Select|Wrap|Line Numbers
  1. DECLARE @Sql nvarchar(100)
  2. DECLARE @localDBName 
  3. DECLARE @Cnt int
  4. set @localDBName='Server1'
  5. set @Cnt=1
  6. WHILE @cnt<9
  7. BEGIN
  8.    SET @Sql ='Select * from '+ @localDBName + '..Customers'
  9.    EXEC @Sql
  10.    set @Cnt=@Cnt+1
  11.    set @localDBName='Server' + convert(char(1),@Cnt)
  12. END
  13.  
Its a ridiculous bit of code that selects the customers records in 8 different servers.
(Server1 through Server8)
But it illustrates the basics of how you use dynamic queries within a while loop.
Delerna's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 783
#10: Jul 30 '09

re: While loop with dynamic sql


Oops, I should have said databases, not servers
Reply