我有一个产生分离输出的查询。我需要将SELECT CASE语句合并为一个,然后将所有输出插入Temp Table
SELECT CASE WHEN #WCV_3.VISIT_1 IS NULL THEN '' ELSE CONVERT(VARCHAR(10), #WCV_3.VISIT_1,110) END
FROM #WCV_3
SELECT CASE WHEN #WCV_3.VISIT_2 IS NULL THEN '' ELSE CONVERT(VARCHAR(10), #WCV_3.VISIT_2,110) END
FROM #WCV_3
等...
SELECT CASE WHEN #WCV_3.VISIT_5 IS NULL THEN '' ELSE CONVERT(VARCHAR(10),
#WCV_3.VISIT_5, 110) END
FROM #WCV_3
我已经尝试了
SELECT * INTO
#WCV_4 (this is my new temp table)
FROM
(
SELECT CASE WHEN #WCV_3.VISIT_1 IS NULL THEN '' ELSE CONVERT(VARCHAR(10), #WCV_3.VISIT_1,110) END
FROM #WCV_3
etc... END)
但发生了2次错误:
"关键字' SELECT'"
附近的语法不正确
AND
"')'""
附近的语法不正确
`
答案 0 :(得分:1)
您的查询应如下所示:
SELECT SomeColumn
INTO #WCV_4
FROM (
SELECT CASE WHEN #WCV_3.VISIT_1 IS NULL THEN '' ELSE CONVERT(VARCHAR(10), #WCV_3.VISIT_1,110) END AS SomeColumn FROM #WCV_3
UNION ALL
SELECT CASE WHEN #WCV_3.VISIT_2 IS NULL THEN '' ELSE CONVERT(VARCHAR(10), #WCV_3.VISIT_2,110) END AS SomeColumn FROM #WCV_3
UNION ALL
SELECT CASE WHEN #WCV_3.VISIT_4 IS NULL THEN '' ELSE CONVERT(VARCHAR(10), #WCV_3.VISIT_4, 110) END AS SomeColumn FROM #WCV_3
UNION ALL
SELECT CASE WHEN #WCV_3.VISIT_5 IS NULL THEN '' ELSE CONVERT(VARCHAR(10), #WCV_3.VISIT_5, 110) END AS SomeColumn FROM #WCV_3
) AS temp
Combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union.
此处可以找到更多信息http://msdn.microsoft.com/en-us/library/ms180026.aspx
我已经测试了这个查询(没有#WCV_3
表)并且它可以工作:
SELECT SomeColumn
INTO #WCV_4
FROM (
SELECT CASE WHEN 'a' IS NULL THEN '' ELSE CONVERT(VARCHAR(10), 'a',110) END AS SomeColumn
UNION ALL
SELECT CASE WHEN 'a' IS NULL THEN '' ELSE CONVERT(VARCHAR(10), 'a',110) END AS SomeColumn
UNION ALL
SELECT CASE WHEN 'a' IS NULL THEN '' ELSE CONVERT(VARCHAR(10), 'a', 110) END AS SomeColumn
UNION ALL
SELECT CASE WHEN 'a' IS NULL THEN '' ELSE CONVERT(VARCHAR(10), 'a', 110) END AS SomeColumn
) AS temp