To fill one variable with a value from a query I can write following:
SET @TargetID = (SELECT TOP 1 ID FROM @bigDataSet ORDER BY date DESC)
To fill multiple variables from this query, eg. something like:
SET (@TargetID, @TargetName) = ....(SELECT TOP 1 ID, [Name] FROM @bigDataSet ORDER BY date DESC)
what can I write?
For example if a new Group_Name was required, then all you need to do is insert it into the Group lookup table rather than modifying the sql code to add the extra group into the list. i am checking that grp name multiple times with different if elseif statements. thats why i created that permanent lookup table.
Assigning multiple values to multiple variablesIf you have to populate multiple variables, instead of using separate SET statements each time consider using SELECT for populating all variables in a single statement. This can be used for populating variables directly or by selecting values from database.
To update multiple columns use the SET clause to specify additional columns. Just like with the single columns you specify a column and its new value, then another set of column and values. In this case each column is separated with a column.
SELECT TOP (1) @TargetID=ID, @TargetName=Name
FROM @bigDataSet
ORDER BY date DESC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With