I’d like to assign the results from two columns to two variables. I have several TSQL statements inside of a cursor loop block. When the @currentId (current row Id), is equal to the max(col1) in tablea, I want to get the value of tableb.col2.
SELECT MAX(col1), b.[col2] from tablea ta JOIN tableb tb on ta.id = tb.id
WHERE ta.id = @currentId
The above doesn’t work of course. Additionally, I’m not sure how to retrieve the values of col1 and col2. Data types are:
col1 int
col2 bit
Any suggestions?
You need to use GroupBy as you are using MAX which is an Agreegate Function.
Note: Above query will fail if the query returns multiple rows.