SQL Server在sql变量中存储多个值
发布时间:2020-05-23 13:43:24 所属栏目:MsSql 来源:互联网
导读:我有以下查询: select * from cars where make in (BMW, Toyota, Nissan) 我想要做的是将where参数存储在SQL变量中. 就像是: declare @caroptions varchar(max);select @caroptions = select distinct(make) from carsforsale;pri
|
我有以下查询: select *
from cars
where make in ('BMW','Toyota','Nissan')
我想要做的是将where参数存储在SQL变量中. 就像是: declare @caroptions varchar(max); select @caroptions = select distinct(make) from carsforsale; print @caroptions; select * from cars where make in (@caroptions) 问题是@caroptions的打印只返回最后一个结果: select distinct(make) from carsforsale; 我希望它存储多个值. 有任何想法吗? 解决方法您可以使用表变量: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) (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python MySQLdb Windows下安装教程及问题解决方法
- 在Python安装MySQL支持模块的方法
- sql-order-by – 如何在Doctrine 2中按计数排序?
- SQLServer触发器创建、删除、修改、查看示例代码
- SQL Server数据类型nvarchar和varchar是不兼容的错误
- sql语句优化之SQL Server(详细整理)
- SQL Server 使用ADSI执行分布式查询ActiveDorectory对象
- 具有多个表和关系的复杂SQL查询
- sql-server – MSSQL – 在SELECT语句中定义列名,然后在WHE
- SQL Server 巧妙的自关联运用实现方法
