【发布时间】:2015-10-12 12:02:55
【问题描述】:
当我使用 UNION 使用 Order by 时出现以下错误。
“ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效,除非还指定了 TOP 或 FOR XML。”
declare @results table (hdate date, name varchar(100), id1 int)
insert into @results select cast('10-01-2015' as date), 'val1 1', 1
insert into @results select cast('10-02-2015' as date), 'val2 2', 2
insert into @results select cast('10-03-2015' as date), 'thanks 1', 3
insert into @results select cast('10-04-2015' as date), 'thanks 2', 3
select (DATENAME(dw, hdate) + ', ' +
DATENAME(mm, hdate) + ' ' +
DATENAME(dd, hdate)) AS h_date, name AS h_name from @results where id1 in (1,2)
order by id1
UNION
SELECT (STUFF((
SELECT ', ' + (DATENAME(dw, hdate) + ', ' +
DATENAME(mm, hdate) + ' ' +
DATENAME(dd, hdate))
FROM @results
WHERE id1 = 3
FOR XML PATH('')
), 1, 2, '')
) AS h_date, 'Giving Day' AS h_name
【问题讨论】:
标签: sql-server tsql