Я уже написал пример кода с комментариями, поэтому я позволю ему объяснить:
/*
Querying sys.system_views to produce a set of rows to use as an example.
No matter how many times the SELECT statement is ran, the results are always the same.
*/
SELECT
ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS sequence,
s.name + '.' + sv.name as name
FROM sys.system_views sv
INNER JOIN sys.schemas s ON s.schema_id = sv.schema_id
WHERE s.name = 'INFORMATION_SCHEMA'
/*
Creating a temporary table using the CTAS principle, as is documented at:
https://docs.microsoft.com/en-us/azure/sql-data-warehouse/sql-data-warehouse-develop-loops
(Since this behaviour was noticed when trying to execute procedures in a defined order.)
*/
CREATE TABLE #list WITH (DISTRIBUTION = ROUND_ROBIN)
AS
SELECT
ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS sequence,
s.name + '.' + sv.name as name
FROM sys.system_views sv
INNER JOIN sys.schemas s ON s.schema_id = sv.schema_id
WHERE s.name = 'INFORMATION_SCHEMA'
/*
The results in the temporary table #list are not the
same as the results of the SELECT statement when ran independently.
No matter how many times the temporary table is created,
the results are in the same order, which again,
is not the resulting order when running the SELECT statement.
*/
SELECT *
FROM #list;
DROP TABLE #list;
Ниже приведен пример изображения результатов запроса рядом.
Запрос результатов SELECT и SELECT из # списка
Проблема легко предотвращается с помощью правильного ORDER BY в функции ROW_NUMBER:
CREATE TABLE #list WITH (DISTRIBUTION = ROUND_ROBIN)
AS
SELECT
ROW_NUMBER() OVER(ORDER BY s.name + '.' + sv.name) AS sequence,
s.name + '.' + sv.name as name
FROM sys.system_views sv
INNER JOIN sys.schemas s ON s.schema_id = sv.schema_id
WHERE s.name = 'INFORMATION_SCHEMA'
Так что мой вопроспочему порядок во временной таблице #list отличается от оператора SELECT?