I’m declaring a varchar variable in T-SQL
declare @var varchar(max);
I have a select query which returns more than one value. Such as:
Select name from table where ...
I want to append each row to this variable. For example my query returns:
name
----------
Joe
Jack
William
Avarel
I want my @var to be this: JoeJackWilliamAvarel
How can I do it? I’m writing this query but it gives me error:
Set @var = (Select name from table where ...)
And the error is:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Thank you
But make sure @var does NOT start as NULL.
@set @var = ''.