Reputation: 7557
I want to store the output of for XML Path in a variable. However this doesn't work:
Declare @Tab dbo.SingleColumnTable
Insert INTO @Tab
Values(
'Jack'
)
Insert INTO @Tab
Values(
'Armageddon'
)
Declare @RetVal varchar(8000) = ''
Select Top 1 @RetVal = Data
From
(
Select ';' + ' ' + Name as 'Data'
From @Tab t2
for xml path('')
) v
As you can see I am trying to set the column name of returned output as Data but it doesn't work. I get error Invalid column name 'Data'.
Upvotes: 9
Views: 14327
Reputation: 166546
Try changing
Select Top 1 @RetVal = Data
From
(
Select ';' + ' ' + Name as 'Data'
From @Tab t2
for xml path('')
) v
to
Select @RetVal =
(
Select ';' + ' ' + Name as Data
From @Tab t2
for xml path('')
)
Upvotes: 16