SQL INSERT @tableVar SELECT ... is only insert
I need to fill a dataset with values from several tables.
I've created a stored procedure
that executes 开发者_运维技巧several SELECT
statements.
Each SELECT
has common parts.
I decided to cache the results of these common parts.
Now I need the SELECT
construct that is capable of outputting the results to a table variable.
INSERT @tableVar SELECT ...
only inserts data but does not select it.
My intention is something like SELECT * OUTPUT SELECTED.* INTO @tableVar FROM ...
Is it possible to perform such operation?
INSERT @tableVar
OUTPUT INSERTED.*
SELECT *
FROM [...]
You can do a select after the first insert:
INSERT @probes
SELECT P.ID, P.ContainerID, P.EstimateID
FROM Probes AS P
WHERE P.LockedBy = @lockedBy
SELECT * FROM @probes
SELECT C.ID, C.Data
FROM Containers AS C
INNER JOIN
(
SELECT DISTINCT P.ContainerID
FROM @probes AS P
) AS X ON C.ID = X.ContainerID
SELECT E.ID, E.ObjectiveID, E.[Time]
FROM Estimates AS E
INNER JOIN
(
SELECT DISTINCT P.EstimateID
FROM @probes AS P
) AS X ON X.EstimateID = E.ID
精彩评论