sql - Set more than 1 value to variable from Select statement -


i'm trying set varchar variable :

declare @var nvarchar(40) set     @var = (select name tmptable name ) 

i want variable store value : 'name1,name2,name3,...' returns error 1 in title: subquery returned more 1 value.

i use variable in paramter of on function

select * tmp2 [dbo].[myfunction](@var, default)  param ..... 

i know can't put more value in variable.i need variable represent more 1 value split value ","

any regard in regards

you can using stuff , for xml:

declare @var nvarchar(40) set     @var =  (     select  distinct stuff((select ',' + name tmptable xml path ('')), 1, 1, '')         tmptable ) 

but, ideally, shouldn't storing comma separated values in varchar. using table-variable work better:

declare @var table (     name    nvarchar (40) )  insert  @var (name) select  name    tmptable 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -