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:
01544224
Views:
21
>>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.


Can you think of why the following SLQ Select (selecting from an XML string):
SELECT @Var1 = [Fld FldName].value('.','varchar(20)'),  [Fld FldName].value('@Name','varchar(20)') AS FldName
  FROM @myxml.nodes('/TABLENAME/Fld') Fld([Fld FldName]) where [Fld FldName].value('@Name','varchar(20)')='Field1'
gives error:
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform