I am writing a query to get data from each DBO on my master.
The query is similar to
DECLARE @variable1 INT
DECLARE @variable2 INT
SET @variable1 = (SELECT column1
FROM database1.dbo.Table_name1
WHERE column1 = value)
SET @variable2 = (SELECT column2
FROM database1.dbo.Table_name2
WHERE column2 = value)
SELECT @variable1/@variable2 AS MyIndex
UNION ALL
SET @variable1 = (SELECT column1
FROM database2.dbo.Table_name1
WHERE column1 = value)
SET @variable2 = (SELECT column2
FROM database2.dbo.Table_name2
WHERE column2 = value)
SELECT @variable1/@variable2 AS MyIndex
However I get the following error
Incorrect syntax near the keyword 'SET'.
on the line where I reset the @variable1 value.
Any help, please?
setyou canselect @variable1= column1 from ...;. This also allows getting multiple values from a singleselect, e.g.select @a =colA, @B = colC / 42 from ...;. Orset @MyIndex1 = ( select column1 from ... ) / ( select column2 from ... );to return the result in one swell foop. Thence onward toselect ( select ... ) / ( select ... ) as MyIndex union all ( select ... ) / ( select ...);without variables.