Besides while loop and Cursors
How can I use CTE table to do n block operations ?
for example :
WITH cte AS (
SELECT 1 AS n
UNION ALL
SELECT n+1
FROM cte
WHERE n+1<= 10
)
INSERT INTO tbl1 (id) select n from cte --till here it's all ok.
INSERT INTO tbl2 (id) select n from cte --{notice tbl2} .fail here ( cause cte usages must come right after the decleration)
But is there anything I can do so the second line will be also include ?
p.s. ( beside keeping it as a temp table)
In general you can’t.
It is only valid for the next statement. You could define a view with the same definition if you want to reuse the definition for multiple statements or materialise it into a temporary table/ table variable yourself to reuse the results.
For the specific case in your question you could do it all in one statement though.