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

How to loop through records

 
   Database Help (Home) -> Programming RSS
Next:  alter table column  
Author Message
staeri

External


Since: Jan 20, 2008
Posts: 15



(Msg. 1) Posted: Sat Feb 16, 2008 10:13 am
Post subject: How to loop through records
Archived from groups: microsoft>public>sqlserver>programming (more info?)

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 please help me with the loop?

// S

 >> Stay informed about: How to loop through records 
Back to top
Login to vote
Alejandro Mesa

External


Since: Jan 16, 2008
Posts: 147



(Msg. 2) Posted: Sat Feb 16, 2008 10:33 am
Post subject: RE: How to loop through records [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You could use an auxiliary table of numbers.

-- SS 2005
-- Idea from Itzik Ben-Gan
DECLARE @WeekIdFrom INT
DECLARE @WeekIdTo INT
DECLARE @t TABLE (WeekId INT NOT NULL PRIMARY KEY)

SET @WeekIdFrom = 12
SET @WeekIdTo = 45

;with
L0 as (select 1 as n union all select 1),
L1 as (select 1 as n from L0 as a, L0 as b),
L2 as (select 1 as n from L1 as a, L1 as b),
L3 as (select 1 as n from L2 as a, L2 as b),
L4 as (select 1 as n from L3 as a, L3 as b),
Numbers as (select row_number() over(order by (select 1)) as n FROM L4)
INSERT INTO @t(WeekId)
SELECT
n
FROM
Numbers
WHERE
n BETWEEN @WeekIdFrom AND @WeekIdTo

SELECT * FROM @t ORDER BY WeekId
GO

Why should I consider using an auxiliary numbers table?
http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-...auxilia

AMB



" " wrote:

> 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 please help me with the loop?
>
> // S
>

 >> Stay informed about: How to loop through records 
Back to top
Login to vote
staeri

External


Since: Jan 20, 2008
Posts: 15



(Msg. 3) Posted: Sat Feb 16, 2008 12:32 pm
Post subject: Re: How to loop through records [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 16 Feb, 21:13, "Plamen Ratchev" wrote:
> If your start and end weeks are in the same year, then the posted solutions
> with utility table with numbers will help. But if the weeks cross years
> (that is start week is in one year, and end week is next year) then you can
> have the case where start week = 40 and end week = 3. In that case it will
> be better to use a calendar table with pre-calculated weeks:http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using...
>
> Then you have to add filters for start and end date to restrict the calendar
> table.
>
> Of course, all those notes are valid only if the meaning of week in your
> case relates to calendar years/dates.
>
> HTH,
>
> Plamen Ratchevhttp://www.SQLStudio.com

Thank you all very much for the help!

I already have tblWeek which will work as the numbers table. The
problem is solved!

// S
 >> Stay informed about: How to loop through records 
Back to top
Login to vote
Tom Cooper

External


Since: Jan 10, 2008
Posts: 463



(Msg. 4) Posted: Sat Feb 16, 2008 1:26 pm
Post subject: Re: How to loop through records [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Create a numbers table - see
http://www.aspfaq.com/show.asp?id=2516
for how to do this and why you want one.

Then you can insert each value without a loop, just one insert statement
which will be much faster, e.g.,

INSERT INTO tblSchedule (WeekID)
SELECT Number
FROM Numbers
WHERE Number BETWEEN @WeekIDFrom AND @WeekIDTo.

Tom

wrote in message

>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 please help me with the loop?
>
> // S
 >> Stay informed about: How to loop through records 
Back to top
Login to vote
Plamen Ratchev

External


Since: Jan 10, 2008
Posts: 1007



(Msg. 5) Posted: Sat Feb 16, 2008 3:13 pm
Post subject: Re: How to loop through records [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

If your start and end weeks are in the same year, then the posted solutions
with utility table with numbers will help. But if the weeks cross years
(that is start week is in one year, and end week is next year) then you can
have the case where start week = 40 and end week = 3. In that case it will
be better to use a calendar table with pre-calculated weeks:
http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-...auxilia

Then you have to add filters for start and end date to restrict the calendar
table.

Of course, all those notes are valid only if the meaning of week in your
case relates to calendar years/dates.

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: How to loop through records 
Back to top
Login to vote
Alejandro Mesa

External


Since: Jan 16, 2008
Posts: 147



(Msg. 6) Posted: Sat Feb 16, 2008 3:13 pm
Post subject: Re: How to loop through records [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Plamen Ratchev,

Good catch!!!

Then I would suggest to use startdate and enddate instead, using the date of
the first date in the week.

DECLARE @WeekIdFrom datetime
DECLARE @WeekIdTo datetime
DECLARE @t TABLE (WeekId INT NOT NULL PRIMARY KEY)

SET @WeekIdFrom = '20080217'
SET @WeekIdTo = '20080323'

;with
L0 as (select 1 as n union all select 1),
L1 as (select 1 as n from L0 as a, L0 as b),
L2 as (select 1 as n from L1 as a, L1 as b),
L3 as (select 1 as n from L2 as a, L2 as b),
L4 as (select 1 as n from L3 as a, L3 as b),
Numbers as (select row_number() over(order by (select 1)) as n FROM L4)
INSERT INTO @t(WeekId)
SELECT
datepart(week, dateadd(week, n - 1, @WeekIdFrom))
FROM
Numbers
WHERE
n <= datediff(week, @WeekIdFrom, @WeekIdTo) + 1

SELECT * FROM @t ORDER BY WeekId
GO


AMB

"Plamen Ratchev" wrote:

> If your start and end weeks are in the same year, then the posted solutions
> with utility table with numbers will help. But if the weeks cross years
> (that is start week is in one year, and end week is next year) then you can
> have the case where start week = 40 and end week = 3. In that case it will
> be better to use a calendar table with pre-calculated weeks:
> http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-...auxilia
>
> Then you have to add filters for start and end date to restrict the calendar
> table.
>
> Of course, all those notes are valid only if the meaning of week in your
> case relates to calendar years/dates.
>
> HTH,
>
> Plamen Ratchev
> http://www.SQLStudio.com
>
 >> Stay informed about: How to loop through records 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Loop through Records and update individual records - Hi, I have the following T-SQL script but I'm not able to convert the logic to T-SQL syntax correctly. I'm stucked with the syntax to do looping What I have in mind is to only UPDATE individual records that have the condition of timebased of "...

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....

Loop through records to Update data? - I am trying to update a table, based on what was in the previous record. Let me try to explain better - the code is at the end. In my table, I have projects, activities, credit amount, debit amount, and starting balance. Each project can have several....

Infinite loop when fetching records in cursor -

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...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
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 ]