Preserving ORDER BY in SELECT INTO

前端 未结 8 778
南笙
南笙 2020-11-27 07:18

I have a T-SQL query that takes data from one table and copies it into a new table but only rows meeting a certain condition:

SELECT VibeFGEvents.* 
INTO Vib         


        
8条回答
  •  没有蜡笔的小新
    2020-11-27 08:09

    I know this is a bit old, but I needed to do something similar. I wanted to insert the contents of one table into another, but in a random order. I found that I could do this by using select top n and order by newid(). Without the 'top n', order was not preserved and the second table had rows in the same order as the first. However, with 'top n', the order (random in my case) was preserved. I used a value of 'n' that was greater than the number of rows. So my query was along the lines of:

    insert Table2 (T2Col1, T2Col2)
      select top 10000 T1Col1, T1Col2
      from Table1
      order by newid()
    

提交回复
热议问题