I have the following select statement…
SELECT ROW_NUMBER() OVER(order by cola) as [id], cola
FROM Table1
That makes up my table of all values I’m wanting to insert as @var, right now it works but I have to specify @var each time…
SELECT @var AS [Cola], (
SELECT COUNT(*)
FROM vwTableA AS Z
WHERE Cola = COALESCE(@var,Cola)
AND NOT EXISTS (
SELECT *
FROM TableB
WHERE Colb = Z.Colb
)
) AS [NewCol1],
(
SELECT COUNT(*)
FROM vwTableB AS Y
INNER JOIN TableC AS C
ON Y.Colc = C.Colc
WHERE Y.Cola = @var
) AS [NewCol2],
(
SELECT COUNT(*)
FROM vwTableC AS X
INNER JOIN TableD AS D
ON X.Colc = D.Colc
WHERE X.Cola = @var
) AS [NewCol3]
So I’m wanting to run this second select through all the values of “cola” from the first Select/Table I showed, instead of having to specify the @var and it only return one row each time. How can I do this?
If you use a Common Table Expression CTE you can use it to join to your other statement