I think I may misunderstand why the with command is used. But can any one see what I am doing wrong.
I want to do a query and use the results for two things. First I want to use the values to make some inserts into another table. Then I want to display the results to the user.
So I have something like this.
With temp as (
Select * from Table1
)
INSERT INTO Table2 (table1_id) select id from temp
SELECT * from temp
And I get
Error: Invalid object name ‘temp’. SQLState: S0002
ErrorCode: 208
Is this not what the with command is for?
From MSDN:
Since the
insertstatement and theselectstatement are two different statements, the CTE is only valid for theinsert.As an alternative, you could consider using an
OUTPUTclause: