SQL Azure doesn't support 'select into' - Is there another way?

孤街浪徒 提交于 2019-12-10 13:53:55

问题


I have a very complicated table I'd like to take a temporary backup of whilst I make some changes. Normally, I'd just do the following:

SELECT  *
INTO    temp_User
FROM    dbo.[User] AS u

Unfortunately I'm using Azure, and it appears this isn't supported:

Msg 40510, Level 16, State 1, Line 2 Statement 'SELECT INTO' is not supported in this version of SQL Server.

Is there a way to re-create this feature into a function, potentially? I could do this by scripting the table, creating it and then inserting data using a select statement but given how frequently I use Azure, and how many databases I need to work on in this area this is very unwieldy.


回答1:


The new Azure DB Update preview has this problem resolved:

The V12 preview enables you to create a table that has no clustered index. This feature is especially helpful for its support of the T-SQL SELECT...INTO statement which creates a table from a query result.

http://azure.microsoft.com/en-us/documentation/articles/sql-database-preview-whats-new/




回答2:


Azure requires a clustered index on all tables, therefore SELECT INTO is not supported.

You'll have to:

CREATE TABLE temp_User () --fill in table structure
INSERT INTO temp_User
SELECT *
FROM dbo.[User]

To script table easily you can write your own or use one of the answers to this question:

Script CREATE Table SQL Server

Update: As Jordan B pointed out, V12 will include support for heaps (no clustered index requirement) which means SELECT INTO will work. At the moment V12 Preview is available, Microsoft of course only recommends upgrading with test databases.




回答3:


Unfortunately it cant be done. Here is how I worked around it:

  1. Open SQL Server Management Studio
  2. Right click on the table
  3. Select Script as ... Create Table
  4. Edit the generated script to change the table name to what you specified in your query
  5. Execute your query



回答4:


INSERT INTO temp_User
SELECT * FROM dbo.[User] 

You can try the above. It's basically a select that is applied to an insert statement

http://blog.sqlauthority.com/2011/08/10/sql-server-use-insert-into-select-instead-of-cursor/




回答5:


Lets assume you have a table with Id, Column1 and Column2. Then this could be your solution

CREATE TABLE YourTableName_TMP ....
GO
SET IDENTITY_INSERT YourTableName_TMP ON 
GO
INSERT INTO YourTableName_TMP
([Id] ,[Column1] ,[Column2])      
SELECT [Id] ,[Column1] ,[Column2]
FROM 
(
    SELECT * 
    FROM
    (
        SELECT  [Id] ,[Column1] ,[Column2] ROW_NUMBER() OVER(ORDER BY ID DESC) AS RowNum
        FROM YourTableName
    )
    WHERE RowNum BETWEEN 0 AND 500000
)
GO
SET IDENTITY_INSERT YourTableName_TMP OFF
GO

First you create a temporary table and then you insert rows windowed. It's a mess, I know. My experiences are, that executing this using SQL Server Management Studio from a client makes approximately 200.000 rows a minute.




回答6:


As wrote above - you need to rewrite your query from using select into to create table like

It is my sample. Was :

        select  emrID, displayName --select into
        into #tTable
        from emrs

        declare @emrid int
        declare @counter int = 1
        declare @displayName nvarchar(max)

        while exists (select * from #tTable)
        begin
            -- some business logic
            select top 1  @displayName = displayname
            from #tTable
            group by displayname
            update emrs  set groupId = @counter where @displayName = displayname    
            delete #tTable
            where @displayName = displayname
            set @counter = @counter + 1 
        end
        drop table #tTable

Modified :

        CREATE TABLE #tTable ([displayName] nvarchar(max)) --create table
        INSERT INTO #tTable -- insert to next select  :

        select  displayName            
        from emrs

        declare @emrid int
        declare @counter int = 1
        declare @displayName nvarchar(max)

        while exists (select * from #tTable)
        begin
            -- some business logic
            select top 1  @displayName = t.displayName
            from #tTable as t
            group by t.displayname
            update emrs  set groupId = @counter where @displayName = displayname    
            delete #tTable
            where @displayName = displayname
            set @counter = @counter + 1 
        end
        drop table #tTable

Do not forget to drop your temp table. Also, you can find more simple example with description here : http://www.dnnsoftware.com/wiki/statement-select-into-is-not-supported-in-this-version-of-sql-server



来源:https://stackoverflow.com/questions/18818394/sql-azure-doesnt-support-select-into-is-there-another-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!