我有一些在我的选择查询中硬编码的数据.
SQL如下:
SELECT 'ZZ0027674', 'ZZ0027704', 'ZZ0027707', 'ZZ0027709', 'ZZ0027729', 'ZZ0027742', 'ZZ0027750'
不幸的是,它不显示数据.它只返回7列,每列都有每个值.我只想要一个具有不同值的列.
请提供不同的解决方案来显示数据?
您可以使用VALUES
,也就是表值构造函数,子句用于硬编码值:
SELECT * FROM (VALUES('ZZ0027674'),('ZZ0027704'),('ZZ0027707'), ('ZZ0027709'),('ZZ0027729'),('ZZ0027742'), ('ZZ0027750') ) AS sub(c)
LiveDemo
警告:此限制最多为1000行并适用于SQL Server 2008+
.对于较低版本,您可以使用UNION ALL
.
编辑:
如果有人可以给我开关,可以加分吗?
SELECT col FROM (SELECT 'ZZ0027674','ZZ0027704','ZZ0027707', 'ZZ0027709','ZZ0027729','ZZ0027742','ZZ0027750' ) AS sub(v1,v2,v3,v4,v5,v6,v7) UNPIVOT ( col for c in (v1,v2,v3,v4,v5,v6,v7) ) AS unpv;
LiveDemo2
使用联合:
SELECT 'ZZ0027674' union all SELECT 'ZZ0027704' union all SELECT 'ZZ0027707' union all SELECT 'ZZ0027709' union all SELECT 'ZZ0027729' union all SELECT 'ZZ0027742' union all SELECT 'ZZ0027750'