Question

Apologies for the simplicity of this post, unfortunately, i'm not a SQL guru so this is beyond my meager skillset.

We have a table that contains schedule information for a webapp. Every month, we use the GUI to copy the next month's schedule across one day at a time. Instead of this process, I'd like to run a SQL script that does the same thing for me.

Take the results of the following query (seen below) which fetches about 340 rows

 select * from GSTProdSchedule where ShiftStartDate = 'd/m/yyyy 12:00:00 AM'

and insert a duplicate of every row for each day of the next month (modifying the three columns on the right).

Here's an example of the current data:

ID          GSTArea_ID  GSTShift_ID HourNumber  ShiftStartDate          HourStartTime           ShiftEndTime           
----------- ----------- ----------- ----------- ----------------------- ----------------------- ----------------------- 
600543      2           1           1           2014-03-03 00:00:00.000 2014-03-03 04:30:00.000 2014-03-03 16:00:00.000
600544      2           1           2           2014-03-03 00:00:00.000 2014-03-03 05:30:00.000 2014-03-03 16:00:00.000
600545      2           1           3           2014-03-03 00:00:00.000 2014-03-03 06:30:00.000 2014-03-03 16:00:00.000
600546      2           1           4           2014-03-03 00:00:00.000 2014-03-03 07:30:00.000 2014-03-03 16:00:00.000
600547      2           1           5           2014-03-03 00:00:00.000 2014-03-03 08:30:00.000 2014-03-03 16:00:00.000

Thanks!

No correct solution

OTHER TIPS

A way for doing that instead of using cursors, is by selecting the data in the specified range and by using the DATEADD function to add a month for each record of the result set which you want to be inserted as new records

Code sample for what you asks for (I haven't tested it)

INSERT INTO GSTProdSchedule
(GSTArea_ID, GSTShift_ID, HourNumber, ShiftStartDate, HourStartTime, ShiftEndTime)
select GSTArea_ID, GSTShift_ID, HourNumber,
DATEADD(m, 1, ShiftStartDate),
DATEADD(m, 1, HourStartTime),
DATEADD(m, 1, ShiftEndTime)
from GSTProdSchedule
where ShiftStartDate between '03/01/2014' and '04/01/2014'

I'm not a fan of cursor and I don't like to use unless I have to. Hope that works as you like

Update: I'm assuming that you are using MS SQL-Server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top