declare @ShipMethodNameList nvarchar(1000) = '' set @ShipMethodNameList = stuff ( ( select distinct ',[' + cast(ShipMethodID as varchar(100)) + '] as [' + Name + ']' from Purchasing.ShipMethod for xml path('') ), 1, 1, '')That certainly works, but usually can be done simply by doing this...where SQL Server will concatenate the string for each row the engine encounters.
declare @otherlist varchar(8000) = '' select @OtherList = @OtherList + '[' + cast(ShipMethodID as varchar(2)) + '] as [' + Name + '],' from Purchasing.ShipMethod set @OtherList = substring(@OtherList,1,len(@OtherList)-1)The FOR XML PATH has great uses, but in this case it can also be done a bit easier.