我发现左外部将 UNPIVOT 结果加入到完整的字段列表中,方便地从 INFORMATION_SCHEMA 中提取,在某些情况下可以实际解决这个问题。
-- test data
CREATE TABLE _t1(name varchar(20),object_id varchar(20),principal_id varchar(20),schema_id varchar(20),parent_object_id varchar(20),type varchar(20),type_desc varchar(20),create_date varchar(20),modify_date varchar(20),is_ms_shipped varchar(20),is_published varchar(20),is_schema_published varchar(20))
INSERT INTO _t1 SELECT 'blah1', 3, NULL, 4, 0, 'blah2', 'blah3', '20100402 16:59:23.267', NULL, 1, 0, 0
-- example
select c.COLUMN_NAME, Value
from INFORMATION_SCHEMA.COLUMNS c
left join (
select * from _t1
) q1
unpivot (Value for COLUMN_NAME in (name,object_id,principal_id,schema_id,parent_object_id,type,type_desc,create_date,modify_date,is_ms_shipped,is_published,is_schema_published)
) t on t.COLUMN_NAME = c.COLUMN_NAME
where c.TABLE_NAME = '_t1'
</pre>
输出看起来像:
+----------------------------------+----------+
| COLUMN_NAME | 价值 |
+----------------------------------+----------+
| 姓名 | 废话1 |
| object_id | 3 |
| principal_id | 空 | <======
| schema_id | 4 |
| parent_object_id | 0 |
| 类型 | 废话2 |
| 类型描述 | 废话3 |
| 创建日期 | 20100402 16:59:23.26 |
| 修改日期 | 空 | <======
| is_ms_shipped | 1 |
| is_published | 0 |
| is_schema_published | 0 |
+----------------------------------+----------+