0

Given the following code:

declare @StatementTable table 
                        (
                            ID int identity not null,
                            StatementText nvarchar(max) not null
                        );

insert into @StatementTable (StatementText)
    select
        StatementToExecute
    from
        dbo.MyTable
    order by
        MyColumn

As long as the dbo.MyTable does not change and MyColumn is unique, will the ID values in the @StatementTable table variable be consistent from execution to execution?

In other words, does providing an order by clause guarantee consistent order of insert?

ORDER BY Clause

When used with a SELECT...INTO statement to insert rows from another source, the ORDER BY clause does not guarantee the rows are inserted in the specified order.

If it matters, I'm on SQL Server 2017...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Josh Jay
  • 1,240
  • 2
  • 14
  • 26

0 Answers0