Okay, so I have this T-SQL statement:
SELECT
COUNT(DISTINCT RentalNo) as Count
FROM
RoomRental AS rr
LEFT JOIN
RoomLinks AS r
On (r.RoomNo1 = rr.RoomNo OR r.RoomNo2 = rr.RoomNo)
AND r.CostCentreNo = rr.CostCentreNo
WHERE Cancelled = 0
AND (rr.RoomNo = @RN OR r.RoomNo2 = @RN OR r.RoomNo1 = @RN)
AND rr.CostCentreNo = @CCN
AND StartDate = @StartDate
AND DATEADD(minute,0-SetupTime,StartTime) < @EndBlock
AND DATEADD(minute,BreakdownTime,EndTime) > @StartBlock)
Is there any way I can store SELECT *, of it, and then quickly (without applying the statement again), get the RentalNos, or apply another AND to the WHERE clause?
Note, this is just an example of my SQL query, I currently have a loop going around, as it goes through all time blocks, (after first querying the first ending).
Basically, there is a lot of similar SQL query repetition. And it would be great If I could store once and manipulate that instead.
Cheers,
Paul
Use
SELECT INTOand store the results in a temporary table (the # prefix is used for a temporary table)Then you can reference #table just like any other physically stored table, join to it, etc.