I have below 2 tables
Table1
Plant
-----
TRP1
DEP1
Table2
Config
------
84ROC20
100ROC20
and 2 textboxes
1.Start date(datetime) : 2012-08-01 00:00:00.000
2.Enddate(datetime):2012-10-01 00:00:00.000
I want to have below table as a result with 3 columns
Plant Config Time
----- ------ -------
TRP1 84ROC20 2012-08-01 00:00:00.000
TRP1 84ROC20 2012-09-01 00:00:00.000
TRP1 84ROC20 2012-10-01 00:00:00.000
DEP1 84ROC20 2012-08-01 00:00:00.000
DEP1 84ROC20 2012-09-01 00:00:00.000
DEP1 84ROC20 2012-10-01 00:00:00.000
TRP1 100ROC20 2012-08-01 00:00:00.000
TRP1 100ROC20 2012-09-01 00:00:00.000
TRP1 100ROC20 2012-10-01 00:00:00.000
DEP1 100ROC20 2012-08-01 00:00:00.000
DEP1 100ROC20 2012-09-01 00:00:00.000
DEP1 100ROC20 2012-10-01 00:00:00.000
Can you please help to have this table
I’m assuming SQL Server 2005+ (using a recursive CTE for convenience), but this should help you out:
This will give the output below for your test data:
Essentially, the trick here is to build the Dates table on the fly and then cross-join that with Plant and Config. You could build the Dates table in a variety of other ways, such as with a tally table, cursor, while-loop, in asp.net itself, etc. I like the ease of a recursive CTE here, though I’m assuming a small number of dates need to be generated. More than 100 dates will require that maxrecursion be set, if not picking another method entirely if performance is a problem.