Using query results as a table

is it possible to use query results as a table without saving the results as a table? That means, changing a table updates the first query and updates the second query based on the first

How to post a T-SQL question on a public forum | spaghettidba

Yes. You can use either a derived table or a cte to do that.

SELECT d.col1, d.col2, o.col3, ...
FROM (
<your_first_query_here>
) AS derived d
INNER JOIN other_table o ON o.keycol = d.othercol ...

Or:

;WITH cte_query_1 AS (
SELECT FROM <rest_of_first_query>
)
SELECT c.col1, c.col2, o.col3, ...
FROM cte_query c
INNER JOIN other_table o ON o.keycol = c.keycol

1 Like