有没有更好(更有效)的方式来编写这个查询?似乎应该有一种方法可以只用一个UNION
.
询问:
SELECT 'Value1' as PropertyKey, PropertyValue=(SELECT Value1 FROM MyTable WITH (NOLOCK))
UNION
SELECT 'Value2' as PropertyKey, PropertyValue=(SELECT Value2 FROM MyTable WITH (NOLOCK))
UNION
SELECT 'Value3' as PropertyKey, PropertyValue=(SELECT Value3 FROM MyTable WITH (NOLOCK))
UNION
...
SELECT 'Value100' as PropertyKey, PropertyValue=(SELECT Value100 FROM MyTable WITH (NOLOCK))
最终,我需要我的结果集有 2 列(PropertyKey
和PropertyValue
)。列中的值PropertyKey
将是我表中列的名称,列中的值PropertyValue
将是相应的值。
UNION
如果我总是从同一张表中选择,是否可以只用一个来写这个?