i have variable in sql server like
DECLARE @Country varchar(max)
SET @Country='GB,US'
here country name is stored and separated by comma. i want to change and store the value in
Country variable like ”GB’,’US’,’DE’,’FR”.
so i was trying by using stuff function like
DECLARE @Country varchar(max)
SET @Country='GB,US'
SELECT @Country=STUFF((SELECT ''', ''' + @Country
FOR XML PATH ('')),
1, 2, '')
print @Country
so please me to achieve it. if i could store country name like ”GB’,’US’,’DE’,’FR” this way into country variable then i can issue a query like
select * from my table where country in ( @Country)
please help thanks
You can’t do it this way because
@countryis still seen as a single variable to SQL Server, not an array (there is no such thing as an array in SQL Server).A couple of workarounds:
1 generate dynamic SQL
2 dump the comma-separated values into a split TVF (see this question for an approach, or this blog post for perf comparisons of multiple approaches).