473,473 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

While loop with dynamic sql

31 New Member
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?
May 8 '09 #1
9 41068
Krandor
50 New Member
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.
May 8 '09 #2
SnehaAgrawal
31 New Member
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
May 9 '09 #3
iburyak
1,017 Recognized Expert Top Contributor
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.
May 11 '09 #4
SnehaAgrawal
31 New Member
Thnks for ur reply ..I want to know how can I use the following statement inside while loop
'Select * from '+@localDBName+'..Customers'
May 13 '09 #5
pankajvtcse
10 New Member
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
Jul 16 '09 #6
Delerna
1,134 Recognized Expert Top Contributor
@SnehaAgrawal
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
Jul 16 '09 #7
SnehaAgrawal
31 New Member
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.
Jul 29 '09 #8
Delerna
1,134 Recognized Expert Top Contributor
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.
Jul 29 '09 #9
Delerna
1,134 Recognized Expert Top Contributor
Oops, I should have said databases, not servers
Jul 30 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
7
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
1
by: pauljturner99 | last post by:
Hi, I'm trying to pass a parameter from a for loop to the nested while loop but only the first counter is passed. Here is the code: dim ctr redim ctr(5) ctr(0) = 2 ctr(1) = 4 ctr(2) = 6
3
by: libsfan01 | last post by:
hi all in my js code i have a while loop contained within a while loop executed on the basis of a conditional if statement, what i want to do is end the entire function on the last execution on...
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
6
by: mgcclx | last post by:
For loop and while loop. which one is faster? I see many articles fighting over it and different people come up with different results.
1
by: somenath | last post by:
Hi All, I have doubt regarding how compiler understands about while loop. For example the bellow mentioned code produce the output as mentioned bellow. #include<stdio.h> int main(void) {
3
by: numlock00 | last post by:
I have a nested 'while' loop that won't repeat, no matter how many times the outer loop repeats. The outer loop reads through an array of elements; the inner loop Ithe 'while' loop) is supposed to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.