472,977 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,977 software developers and data experts.

"for" loop

hello

Does exist in SQL language "for" loop ? If yes, what syntax does it has ?

best wishes
Adam
Aug 17 '05 #1
9 162477
No - see WHILE and "Cursors" in Books Online. But in SQL, a loop is
often not a good solution - people may use them when they should use a
single, set-based statement such as UPDATE or DELETE.

Of course, there are times when a while or cursor loop is the best
solution, so you might have a good reason to do this - if you can give
more details of what you need to achieve, someone may be able to
comment on whether a loop is appropriate for you or not.

Simon

Aug 17 '05 #2
Mostly it pays to find a set based solution rather than write loops. If
you need to loop then TSQL has WHILE.

--
David Portas
SQL Server MVP
--

Aug 17 '05 #3
I try to substitute "for" loop by this excample:

declare @i int
while (@i<10)
begin
insert into dbname.dbo.table_name values @i
@i=@i+1
end

but in line @i=@i+1 ocured an error. Why ?
I know - it is stupid reason to use "for" loop , but what shoul I changed to
work this example ?
What are better ways to do such task ?

Aug 17 '05 #4
A common solution is to keep a table of numbers in your database
(single column of numbers from 0 to some very large number). This can
help avoid loops in many places:

INSERT INTO table_name (...)
SELECT num, ...
FROM Numbers
WHERE num BETWEEN 1 AND 10

Of course you'll still probably use a loop to populate the Numbers
table, but that only has to be done once and at install time, not at
runtime.

--
David Portas
SQL Server MVP
--

Aug 17 '05 #5
Numbers table:

CREATE TABLE numbers (num INTEGER NOT NULL CONSTRAINT pk_numbers
PRIMARY KEY) ;

INSERT INTO numbers (num) VALUES (0) ;

WHILE (SELECT MAX(num) FROM numbers)<65535
INSERT INTO numbers
SELECT num+(SELECT MAX(num)+1 FROM numbers)
FROM numbers ;

--
David Portas
SQL Server MVP
--

Aug 17 '05 #6
As David suggested, a numbers table is definitely a useful tool:

http://www.aspfaq.com/show.asp?id=2516

A calendar table is another good one:

http://www.aspfaq.com/show.asp?id=2519

Simon

Aug 17 '05 #7
Hi David,
We can do this simple trick

Select identity(int,1,1) MyId into MyTable from
anySystemTable,anySystemTable,anySystemTable .......

With warm regards
Jatinder Singh

Aug 17 '05 #8
adam (er******@wp.pl) writes:
I try to substitute "for" loop by this excample:

declare @i int
while (@i<10)
begin
insert into dbname.dbo.table_name values @i
@i=@i+1
end

but in line @i=@i+1 ocured an error. Why ?


You need to say SET or SELECT in front. I prefer SELECT, as you can assign
more that one variable in one bang:

SELECT @a = 1, @b = @c + d, @t = NULL


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Aug 17 '05 #9
Here is a way to gemerate the Sequence table without proprietary code
and create the rows in parallel instead of one at a time.

WITH
(SELECT 0
UNION ALL
SELECT 1
..
UNION ALL
SELECT 9) AS Digits(digit)
SELECT D1.digit + 10*D2.digit + 100*D3.digit + 1000*D4.digit + ..
FROM Digits AS D1, Digits AS D2, Digits AS D3, Digits AS D4, ..
WHERE (D1.digit + D2.digit + D3.digit + D4.digit + ..) > 0;

IDENTITY has be done one at a time, while this can be parallelized and
can generate numbers in sets. The CTE can be repalced with a TABLE or
VIEW.

Aug 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
1
by: deko | last post by:
This sub pulls Outlook Appointments into a table. The problem is I want to limit the import to Location = Boston I'm not sure how to code that into the For... To loop - As it is now, I get the...
3
by: songie D | last post by:
would it be possible to sort of engineer some sort of preprocessor macro that does a 'for' loop. i.e. for where you would normally use a normal for loop, but when it is known ay compile time whay...
12
by: Robbie Hatley | last post by:
I'm getting a bizarre error with this code: ... case WM_COMMAND: { switch (LOWORD(wParam)) // switch (control ID) { ... case IDC_RAIN_ENABLE_SIMPLE: {
5
by: Fabian Vilers | last post by:
Hi again... I'm wondering what could be better in terms of performance between: var my_array = new Array(); // populate array for (index in my_array) { // do something with my_array
10
by: s.lipnevich | last post by:
Hi All, I apologize if this was brought up before, I couldn't find any "prior art" :-). On more than one occasion, I found myself wanting to use a "conditional loop" like this (with "Invalid...
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
4
by: MDR | last post by:
Hello I have three "for" loops, two nested into the outer one and they depend on each other, like this: for (x=1; x<100; x++) { .... for (i=1; i<10; i++) {....} for (j=1; j<10;...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.