sirbounty
asked on
Inserting records; increment weekly
Looking for a function in SQL (2008) where I can repeatedly enter a new row, incrementing two date fields (by 7 days each time)...
Insert into MyTable Values (Field1, Field2, StartDate, EndDate)
With start date beginning at 02/04/2013, end date beginning at 2/10/2013 and both continuing until year-end (or so)...
Insert into MyTable Values (Field1, Field2, StartDate, EndDate)
With start date beginning at 02/04/2013, end date beginning at 2/10/2013 and both continuing until year-end (or so)...
ASKER
cursor?
Here is a sample code:
Giannis
DECLARE @MyTable Table
(
StartDate DATETIME,
EndDate DATETIME
)
DECLARE @InitialStartDate DATETIME
DECLARE @InitialENDDate DATETIME
DECLARE @StopDate DATETIME
SET @InitialStartDate = '20130402'
SET @InitialENDDate = '20131002'
SET @StopDate = '20131231'
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
Insert into @MyTable Values (@InitialStartDate,@InitialENDDate)
DECLARE IncrementalInsert CURSOR FOR
SELECT MAX(StartDate), MAX(EndDate)
FROM @MyTable
OPEN IncrementalInsert
FETCH NEXT FROM
IncrementalInsert
INTO @StartDate, @EndDate
WHILE @StartDate<@StopDate
BEGIN
SET @StartDate = DATEADD(dd,7,@StartDate)
SET @EndDate = DATEADD(dd,7,@EndDate)
--INSERT INTO @MyTable Values ( @StartDate, @EndDate)
SELECT * FROM @MyTable
FETCH NEXT FROM IncrementalInsert INTO @StartDate, @EndDate
END
CLOSE IncrementalInsert
DEALLOCATE IncrementalInsert
Giannis
In my scenario i create a table variable in order to store the dates. You don't need to do that as you already have the table, but my code is just a demonstration.
For simplicity i have only the date fields but you may have your other fields too.
Then i declare my starting dates and the date after which i need the loop to stop.
After that, i insert the first row and in the loop i insert the dates incremented by 7.
When my start date becomes greater than the 'StopDate' i quit the loop.
I hope it is clear what i am doing....
Giannis
For simplicity i have only the date fields but you may have your other fields too.
Then i declare my starting dates and the date after which i need the loop to stop.
After that, i insert the first row and in the loop i insert the dates incremented by 7.
When my start date becomes greater than the 'StopDate' i quit the loop.
I hope it is clear what i am doing....
Giannis
ASKER
Still going through it, but that helps some.
When I run it, it doesn't seem to be incrementing as is.
I get the date setup, and the loop, but I'm not sure about the table setup.
In your example, you declare a table with only start & end dates.
Would I add the other columns in my table to that (and their data types)?
How would I reference that to my actual table name?
Does this section
SELECT MAX(StartDate), MAX(EndDate)
FROM @MyTable
Actually state to grab what's already in the rows? I'm okay with that method, just trying to understand the code...
I'm having trouble with the while definition as well...
WHILE @StartDate<@StopDate
StartDate will always be < StopDate, won't it?
When I run it, it doesn't seem to be incrementing as is.
I get the date setup, and the loop, but I'm not sure about the table setup.
In your example, you declare a table with only start & end dates.
Would I add the other columns in my table to that (and their data types)?
How would I reference that to my actual table name?
Does this section
SELECT MAX(StartDate), MAX(EndDate)
FROM @MyTable
Actually state to grab what's already in the rows? I'm okay with that method, just trying to understand the code...
I'm having trouble with the while definition as well...
WHILE @StartDate<@StopDate
StartDate will always be < StopDate, won't it?
The table variable is just to show you an example. You should actualy use your table.
The select max is to just get your starting dates if you actually have data in your table.
If you see every time you enter in the loop it increments the @StartDate so at some point it will be greater than the @StopDate.
Giannis
The select max is to just get your starting dates if you actually have data in your table.
If you see every time you enter in the loop it increments the @StartDate so at some point it will be greater than the @StopDate.
Giannis
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That code gives me :Incorrect syntax near 'testdata'
on the line that reads:
INSERT INTO dbo.tblcollaboncall (NewID, startDate, endDate, 'testdata',1,0)
on the line that reads:
INSERT INTO dbo.tblcollaboncall (NewID, startDate, endDate, 'testdata',1,0)
ASKER
Ah, took me a bit to figure it out, but that ended up being quick and simple once I got my head wrapped around it - thanx! :^)
Sweet!
If you need to read data from an existing table -- that is, take row(s) from an existing table and combine them with all the dates -- you can do this:
INSERT INTO dbo.MyTable ( field1, field2, startDate, endDate )
SELECT tn.field1, tn.field2, DATEADD(DAY, num * 7, @startDate), DATEADD(DAY, num * 7 + 6, @startDate)
FROM dbo.table_name tn
CROSS JOIN cteTally
WHERE
num BETWEEN 0 AND DATEDIFF(DAY, @startDate, @last_date_to_generate) / 7 - 1
--AND tn.col_name = 'some_value' AND ...
ORDER BY
3
If you need to read data from an existing table -- that is, take row(s) from an existing table and combine them with all the dates -- you can do this:
INSERT INTO dbo.MyTable ( field1, field2, startDate, endDate )
SELECT tn.field1, tn.field2, DATEADD(DAY, num * 7, @startDate), DATEADD(DAY, num * 7 + 6, @startDate)
FROM dbo.table_name tn
CROSS JOIN cteTally
WHERE
num BETWEEN 0 AND DATEDIFF(DAY, @startDate, @last_date_to_generate) / 7 - 1
--AND tn.col_name = 'some_value' AND ...
ORDER BY
3
Giannis