Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
nice123ej
Starting Member
48 Posts |
Posted - 2008-10-17 : 09:46:35
|
| hii have a table that will take list of datesi.e. the table will have records for each day in a periodexample : this table will take 5 records for period between 24 Jun 2008 and 28 Jun 2008XTable xid xdate-------------------1 24 Jun 20082 25 Jun 20083 26 Jun 20084 27 Jun 20085 28 Jun 2008this table will be built based on start date (24 Jun 2008) and end date (28 Jun 2008)i built it using WHILE statementso i loop for days between this period and insert them one by onei dont think this is the best way to do that as if the period is longer this will not be so efficient e.g. for period (1 Jan 1964) to (31 dec 2037)is there any way i can do it using 1 insert statementlike insert into XTableselect date from ....where date between '1 Jan 1964' and '31 dec 2037'thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-17 : 09:51:05
|
you can CREATE TABLE XTable(xid int identity(1,1),xdate datetime)GODECLARE @StartDate datetime, @EndDate datetimeSELECT @StartDate='24 Jun 2008',@EndDate='28 Jun 2008';With Date_CTE (Date) AS(SELECT @StartDateUNION ALLSELECT DATEADD(d,1,Date)FROM Date_CTEWHERE DATEADD(d,1,Date)<=@EndDate)INSERT INTO XTable(xdate)SELECT Date FROM Date_CTE |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-17 : 09:54:11
|
| with temp1 as (select cast('24 Jun 2008' as datetime) as TheDateUNION ALLselect dateadd(day,1,TheDate) from temp1where TheDate <= cast('28 Jun 2008' as datetime))select * from temp1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-17 : 09:58:30
|
quote: Originally posted by hanbingl with temp1 as (select cast('24 Jun 2008' as datetime) as TheDateUNION ALLselect dateadd(day,1,TheDate) from temp1where TheDate <= cast('28 Jun 2008' as datetime))select * from temp1
by 3 minutes |
 |
|
|
nice123ej
Starting Member
48 Posts |
Posted - 2008-10-18 : 01:34:36
|
| Sorry, but your solution did not worki get this error now when i try bigger period (02 Jan 2002) to (28 Jun 2008)Msg 530, Level 16, State 1, Line 4The statement terminated. The maximum recursion 100 has been exhausted before statement completion. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-18 : 02:04:37
|
quote: Originally posted by nice123ej Sorry, but your solution did not worki get this error now when i try bigger period (02 Jan 2002) to (28 Jun 2008)Msg 530, Level 16, State 1, Line 4The statement terminated. The maximum recursion 100 has been exhausted before statement completion.
try specifying an explicit recursion levelCREATE TABLE XTable(xid int identity(1,1),xdate datetime)GODECLARE @StartDate datetime, @EndDate datetimeSELECT @StartDate='24 Jun 2008',@EndDate='28 Jun 2008';With Date_CTE (Date) AS(SELECT @StartDateUNION ALLSELECT DATEADD(d,1,Date)FROM Date_CTEWHERE DATEADD(d,1,Date)<=@EndDate)INSERT INTO XTable(xdate)SELECT Date FROM Date_CTEOPTION (MAXRECURSION 10000); |
 |
|
|
|
|
|
|
|