I know this has already been solved, but I thought it might be educational to spell out the answer and an alternative approach.
DECLARE @PhaseDate DATETIME;
DECLARE @DueDate DATETIME;
DECLARE @CUST_ID BIGINT;
DECLARE @FLAT_ID BIGINT;
--Temporary table to accumulate the results into.
CREATE TABLE #holdingTable (
PhaseDate DATETIME,
DueDate DATETIME,
CUST_ID BIGINT,
FLAT_ID BIGINT
--List the other columns returned from rptPhaseAllDeatils here.
);
--Fast_forward cursors are faster than default ones.
DECLARE toLast CURSOR LOCAL FAST_FORWARD FOR
SELECT FLAT_ID, CUST_ID, DueDate, PhaseDate
FROM TBL_FLATMSTR
WHERE TOTAL_VALUE != NULL;
OPEN toLast;
--The "WHILE(1 = 1) BEGIN ... FETCH ... IF (@@Fetch_Status > 0) BREAK" pattern
--means we don't need to repeat the FETCH statement (so less typing).
WHILE (1 = 1) BEGIN
FETCH toLast INTO @FLAT_ID, @CUST_ID, @DueDate, @PhaseDate;
IF (@@FETCH_STATUS > 0) BREAK;
--Append output of "rptPhaseAllDeatils" sproc to our temporary table.
INSERT INTO #holdingTable
EXEC rptPhaseAllDeatils @PhaseDate, @DueDate, @CUST_ID, @FLAT_ID;
END
CLOSE toLast;
DEALLOCATE toLast;
--Return the accumulated results.
SELECT * FROM #holdingTable;
A faster alternative would be to create a new version of
rptPhaseAllDeatils
that incorporates the
TOTAL_VALUE != NULL
filter. That would be much faster than mucking about with a temporary table and a cursor.