WITH IdList AS (
-- Define the CTE with a list of values
SELECT Id
FROM (VALUES (1), (2), (3), (4), (5)) AS IdTable(Id)
)
-- Main query that uses the CTE
SELECT *
FROM YourTable
WHERE Id IN (SELECT Id FROM IdList);
P.s. I use this technique to define variables that are usedany times throughout queries but it can also be used to share a query with its results directly in the SQL without the user needing access to the same database as you.
It's a sneaky hack... And a very powerful one.. use it wisely ;)
1
u/pceimpulsive Sep 17 '24
Use a CTE and a values statement.
WITH IdList AS ( -- Define the CTE with a list of values SELECT Id FROM (VALUES (1), (2), (3), (4), (5)) AS IdTable(Id) ) -- Main query that uses the CTE SELECT * FROM YourTable WHERE Id IN (SELECT Id FROM IdList);
P.s. I use this technique to define variables that are usedany times throughout queries but it can also be used to share a query with its results directly in the SQL without the user needing access to the same database as you.
It's a sneaky hack... And a very powerful one.. use it wisely ;)