Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Set selected value to a variable
Message
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01544218
Message ID:
01544220
Views:
28
>Hi,
>
>The only way I found that I can assign a value selected in SQL Select to a variable as following:
>
>
>select @MyVar = (select MyColumn1 from MyTable where MyColumn2 = 'SomeValue')
>
>
>Is the above the only way to do it? Or there is a better approach? TIA.

Another way is
select @MyVar = MyColumn1 from myTable
The difference between these two methods is that the first will always change the @MyVar variable. In the second case, if the select statement does not produce rows, the original value of @MyVar will not change.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform