Does ORDER BY work in SELECT @some_var=
SQL Server 2008r2 Express SP1. I need to make a sting with list of al开发者_C百科l distinct dates in some field for EXEC 'dynamic SQL'. Of course, dates should be in right order. The code below works fine but my boss says that on big tables ORDER BY wouldn't work in some cases because of @pivot_list =... in the SELECT statement. He has worked with SqlServer2000 and there this code even wouldn't be compiled. Can anybody tell me whether this will work on SQL Server 2008r2?
USE tempdb
GO
DECLARE @pivot_list varchar(max)
CREATE TABLE #TovarSales(FullDate varchar(8))
INSERT #TovarSales VALUES ('20101010')
INSERT #TovarSales VALUES ('20101210')
INSERT #TovarSales VALUES ('20091010')
INSERT #TovarSales VALUES ('20111111')
INSERT #TovarSales VALUES ('20050505')
SELECT @pivot_list = ISNULL(@pivot_list + ', ', '') + '[' + TS.FullDate + ']'
FROM #TovarSales TS
GROUP BY TS.FullDate
ORDER BY TS.FullDate
select @pivot_list
DROP TABLE #TovarSales
Thanks.
This code DOES work in SQL Server 2008.
The result is
[20050505], [20091010], [20101010], [20101210], [20111111]
精彩评论