?
Solved

SQL 2000 - for Loop to insert a range of dates and values at once

Posted on 2009-02-12
3
Medium Priority
?
573 Views
Last Modified: 2012-05-06
What is the best way to do this?

Have a table tblMax  with DateFund and Maxvalue fields.

Need to fill that table with range of dates from 02/12/2009 - 01/01/2010

DateFund        MaxValue
02/12/2009     0
02/13/2009     0
etc

I guess it would be some For Loop in SQL Analizer or Stored Procedure.

Can anyone write me a quick simple code for it?

Thanks
0
Comment
Question by:Terrace
  • 2
3 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 23626565
declare @tmp datetime

set @tmp = '02/12/2009'

while @tmp <= '01/01/2010'
begin
   insert into yourTable(DateFund, MaxValue)
   values  (@tmp, 0)
   
   set @tmp = dateadd(day, @tmp, 1)
end

0
 
LVL 26

Accepted Solution

by:
tigin44 earned 800 total points
ID: 23626609
a typo error occured...
chenge the dateadd part like the one below

set @tmp = dateadd(day, 1, @tmp)
0
 
LVL 22

Assisted Solution

by:dportas
dportas earned 200 total points
ID: 23626784
CREATE TABLE tblMax (DateFund DATETIME NOT NULL PRIMARY KEY, MaxValue INT NOT NULL);

INSERT INTO tblMax (DateFund, MaxValue) VALUES ('20090212',0);

WHILE @@ROWCOUNT > 0
 INSERT INTO tblMax (DateFund, MaxValue)
 SELECT dt,0 FROM
  (SELECT DATEADD(DAY,DATEDIFF(DAY,t.DateFund,m.DateFund)+1,m.DateFund) dt
  FROM tblMax t,(SELECT MAX(DateFund) FROM tblMax) m(DateFund)) d
 WHERE dt BETWEEN '20090212' AND '20100101';
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question