I’m looking for a solution where I have to create a set of records from one record using data from another table. The table definition:
DECLARE A AS TABLE
(
AID BIGINT NOT NULL,
StartDate DATETIME NOT NULL,
EndDate DATETIME
)
DECLARE B AS TABLE
(
AID BIGINT NOT NULL,
StartDate DATETIME NOT NULL,
EndDate DATETIME NULL
)
The idea is that when A contains:
1 | 01-01-2010 | 01-02-2010
2 | 01-10-2010 | 31-10-2010
and B contains:
1 | 01-01-2010 | 15-01-2010
2 | 15-10-2010 | 20-10-2010
we recieve 5 records:
1 | 01-01-2010 | 15-01-2010
1 | 16-01-2010 | 01-02-2010
2 | 01-10-2010 | 15-10-2010
2 | 16-10-2010 | 20-10-2010
2 | 21-10-2010 | 31-10-2010
Currently we do this with a cursor on A and an inner loop cursor on B, we have to do this in SQLServer (TSQL or in worst case CLR)
Any ideas on how to write this as a select so that the overhead of the cursor disappears?
1 Answer