SQL Server在sql变量中存储多个值

时间:2021-02-09 15:41:21

I have the following query:

我有以下查询:

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

What I want to do is store the where parameters in a SQL variable.

我想要做的是将where参数存储在SQL变量中。

Something like:

就像是:

declare @caroptions varchar(max);
select @caroptions =  select distinct(make) from carsforsale;
print @caroptions;
select * from cars where make in (@caroptions)

Problem is the print of @caroptions only has the last result returned from:

问题是@caroptions的打印只返回最后一个结果:

select distinct(make) from carsforsale;

I want it to store multiple values.

我希望它存储多个值。

Any ideas?

有任何想法吗?

5 个解决方案

#1


7  

You can use a table variable:

您可以使用表变量:

declare @caroptions table
(
    car varchar(1000)
)

insert into @caroptions values ('BMW')
insert into @caroptions values ('Toyota')
insert into @caroptions values ('Nissan')

select * from cars where make in (select car from @caroptions)

#2


3  

I wrote about this here if you want to see it in detail. In the mean time, you can't do it exactly how you are thinking.

如果你想详细看到它,我在这里写了这个。与此同时,你无法完全按照自己的想法去做。

Your choices are:

你的选择是:

Using the LIKE command:

使用LIKE命令:

DECLARE @CarOptions varchar(100)
SET @CarOptions = 'Ford, Nisan, Toyota'

SELECT *
FROM Cars
WHERE ','+@CarOptions+',' LIKE ',%'+CAST(Make AS varchar)+',%'

A spliter function

分离器功能

DECLARE @CarOptions varchar(100)
SET @CarOptions = 'Ford, Nisan, Toyota'

SELECT Cars.*
FROM Cars
JOIN DelimitedSplit8K (@CarOptions,',') SplitString
    ON Cars.Make = SplitString.Item

Dyanmic SQL

动态SQL

DECLARE @CarOptions varchar(100)
SET @CarOptions = 'Ford, Nisan, Toyota'

DECLARE @sql nvarchar(1000)

SET @sql = 'SELECT * ' + 
            'FROM Cars ' + 
            'WHERE Make IN ('+@CarOptions+') '

EXEC sp_executesql @sql

In the mean time your best option is going to be to get rid of the variable completely.

与此同时,您最好的选择是完全摆脱变量。

SELECT * FROM cars WHERE make IN (SELECT make FROM carsforsale );

#3


1  

why not?

为什么不?

SELECT * FROM cars WHERE make IN (SELECT DISTINCT(make) FROM carsforsale)

#4


1  

Fetch 1 value in table and store in variable    
=======================================================================================

Declare @query int

    select @query = p.ProductID From Product p inner join ReOrdering as r on 
    p.ProductID = r.ProductID and r.MinQty >= p.Qty_Available

    print @query

#5


0  

you can use JOIN statement.

你可以使用JOIN语句。

SELECT distinct c.*
FROM cars c
JOIN carsfrosale s
ON s.id = c.fk_s

If you want filter your list of carsforsale you can add

如果你想过滤你的carforsale列表,你可以添加

WHERE s.id in (....)

#1


7  

You can use a table variable:

您可以使用表变量:

declare @caroptions table
(
    car varchar(1000)
)

insert into @caroptions values ('BMW')
insert into @caroptions values ('Toyota')
insert into @caroptions values ('Nissan')

select * from cars where make in (select car from @caroptions)

#2


3  

I wrote about this here if you want to see it in detail. In the mean time, you can't do it exactly how you are thinking.

如果你想详细看到它,我在这里写了这个。与此同时,你无法完全按照自己的想法去做。

Your choices are:

你的选择是:

Using the LIKE command:

使用LIKE命令:

DECLARE @CarOptions varchar(100)
SET @CarOptions = 'Ford, Nisan, Toyota'

SELECT *
FROM Cars
WHERE ','+@CarOptions+',' LIKE ',%'+CAST(Make AS varchar)+',%'

A spliter function

分离器功能

DECLARE @CarOptions varchar(100)
SET @CarOptions = 'Ford, Nisan, Toyota'

SELECT Cars.*
FROM Cars
JOIN DelimitedSplit8K (@CarOptions,',') SplitString
    ON Cars.Make = SplitString.Item

Dyanmic SQL

动态SQL

DECLARE @CarOptions varchar(100)
SET @CarOptions = 'Ford, Nisan, Toyota'

DECLARE @sql nvarchar(1000)

SET @sql = 'SELECT * ' + 
            'FROM Cars ' + 
            'WHERE Make IN ('+@CarOptions+') '

EXEC sp_executesql @sql

In the mean time your best option is going to be to get rid of the variable completely.

与此同时,您最好的选择是完全摆脱变量。

SELECT * FROM cars WHERE make IN (SELECT make FROM carsforsale );

#3


1  

why not?

为什么不?

SELECT * FROM cars WHERE make IN (SELECT DISTINCT(make) FROM carsforsale)

#4


1  

Fetch 1 value in table and store in variable    
=======================================================================================

Declare @query int

    select @query = p.ProductID From Product p inner join ReOrdering as r on 
    p.ProductID = r.ProductID and r.MinQty >= p.Qty_Available

    print @query

#5


0  

you can use JOIN statement.

你可以使用JOIN语句。

SELECT distinct c.*
FROM cars c
JOIN carsfrosale s
ON s.id = c.fk_s

If you want filter your list of carsforsale you can add

如果你想过滤你的carforsale列表,你可以添加

WHERE s.id in (....)