Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Loop in s SQL Query

 
   Database Help (Home) -> Programming RSS
Next:  Unable to create to clustered sql server  
Author Message
Marc Robitaille

External


Since: Feb 17, 2009
Posts: 3



(Msg. 1) Posted: Sat Apr 11, 2009 11:29 am
Post subject: Loop in s SQL Query
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hello,

I converted an old VB6 application to NET. There are several treatments that
I would turn it into stored procedures. One treatment is not particularly
easy to transform. Here is my problem. I have a table that contains the
information of customers. We can identify a customer with a unique key. The
user of the application must select a year in a drop down list eg 2002. The
application has a starting year. The starting year is 1999 and it never
changes because it is a constant. A calculation is made between the two
years. This calculation is to obtain a period. The calculation is as
follows:

2002 + 2 - 1999 = 5

With this value, in VB6, it is providing me am array of 0 to 5 elements.
So, I have to reproduce the same thing but in a table. I must have this:

Custid Year
1 1999 (0)
1 2000 (1)
1 2001 (2)
1 2002 (3)
1 2003 (4)
1 2004 (5)
2 1999 (0)
2 2000 (1)
2 2001 (2)
2 2002 (3)
2 2003 (4)
2 2004 (5)
3 1999 (0)
3 2000 (1)
3 2001 (2)
3 2002 (3)
3 2003 (4)
3 2004 (5)
....

Here is my question. As my knowledge of SQL its limited, is it possible to
do this with an SQL query that acts like a kind of loop?

Thank you!

Marc R.

 >> Stay informed about: Loop in s SQL Query 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 2) Posted: Sun Apr 12, 2009 2:05 pm
Post subject: Re: Loop in s SQL Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> As my knowledge of SQL its limited, is it possible to do this with an SQL query that acts like a kind of loop? <<

No, SQL is declarative and does not like the concept of a loop. We
work with SETS and do them ALL AT ONCE -- no sequential processing. If
you think this weird, loop at LISP and recursion instead of loops.

What we often do is create a table of integers from 1 to (n) and then
JOIN to it for many of things that a procedural language would do in
a loop. We also turn IF-THEN-ELSE logic into CASE expression.

Get a copy of THINKING IN SETS for some help with the mindset
changes. Could be worse; could be LISP Smile

 >> Stay informed about: Loop in s SQL Query 
Back to top
Login to vote
steve dassin

External


Since: Jan 14, 2008
Posts: 132



(Msg. 3) Posted: Sun Apr 12, 2009 3:52 pm
Post subject: Re: Loop in s SQL Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"--CELKO--" wrote in message

>>> As my knowledge of SQL its limited, is it possible to do this with an
>>> SQL query that acts like a kind of loop? <<
>
> No, SQL is declarative and does not like the concept of a loop. We
> work with SETS and do them ALL AT ONCE -- no sequential processing. If
> you think this weird, loop at LISP and recursion instead of loops.

Do you think it makes sense to describe an algebra as 'all at once'?Smile Sql
uses the idea of a tiered architecture as a crutch to remain a primitive
language, immunized from any change. It's a language that has had no
evolution. At least Cobol has evolved:) One rather suspects that sql's 'all
at once' is also a crutch so as to hide any sensible logical explanation of
what is really a language based on the ying and the yang:) Hell if sql were
an algebra then it could even be taught!Smile

Have a happy Easter

www.beyondsql.blogspot.com
 >> Stay informed about: Loop in s SQL Query 
Back to top
Login to vote
alkuzo

External


Since: Apr 13, 2009
Posts: 1



(Msg. 4) Posted: Mon Apr 13, 2009 1:46 pm
Post subject: Re: Loop in s SQL Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Apr 12, 5:52 pm, "steve dassin" wrote:
> Do you think it makes sense to describe an algebra as 'all at once'?Smile Sql
> uses the idea of a tiered architecture as a crutch to remain a primitive
> language, immunized from any change.

BRAVO!

>It's a language that has had no
> evolution. At least Cobol has evolved:)

I love it!
 >> Stay informed about: Loop in s SQL Query 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
While loop? - So I am just starting to learn how to use Database. I need the help of a SQL guru! I have these three tables: CREATE TABLE SB3_ScheduleChange ( emp_num EmpNumType:nvarchar(7) NOT NULL PRIMARY KEY, Monday char(35) NULL, Tuesday char(35) NULL...

How to loop through records - I have @WeekIDFrom and @WeekIDTo as input parameters in a stored procedure. I need to loop through each value between @WeekIDFrom and @WeekIDTo and insert the value into a table, like this: INSERT INTO tblSchedule (WeekID) VALUES (@WeekID) Can someone...

How to loop a temp table? - I have a statement in my stored procedure that has the following as an example - INSERT #TempTableOutput EXEC (@select + @from + @where + @order) How could I then loop through the records of #TempTableOutput to update some blank fields that were..

Loop through Table Records - I'm working on a script that creates a temp table called #tmpList. How can Loop through the records of #tmpList? So far in my using SQL, I've always written SQL statements to just return records and I did my looping with VB. I need to be able to loop....

Date and case loop - My date work but I need to put it in a case loop, can anyone help me. Declare @DayOfWeek int Declare @dayname varchar(10) set @DayOfWeek = datepart(dw, getdate()) case When @DayOfWeek = 1 Then @dayname ='Sunday' When @DayOfWeek = 2 Then @dayname..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]