SQL Server store multiple values in sql variable

后端 未结 6 904
春和景丽
春和景丽 2020-12-29 15:02

I have the following query:

select * 
from cars 
where make in (\'BMW\', \'Toyota\', \'Nissan\')

What I want to do is store the where param

6条回答
  •  死守一世寂寞
    2020-12-29 15:29

    Use CTE for storing multiple values into a single variable.

    ;WITH DATA1 AS 
    (
        select car_name
        from cars 
        where make in ('BMW', 'Toyota', 'Nissan')
    )
    SELECT @car_name = CONCAT(@car_name,',',car_name)
    FROM DATA1
    
    select @car_name
    

提交回复
热议问题