Replacing "select * in" in sql azure?

I need a way to generalize a table and copy its data into a new table - basically the same as SELECT * INTO on regular SQL Server. Is there a way to do this in SQL Azure? At the moment, I only have existing and new table names.

+5
source share
6 answers

I ran into the same problem and the author’s answer is not very detailed, so I’ll talk a little more about how I solved it.

I needed to duplicate tables starting with the given prefix ('from_') into new tables with the prefix ('to _').

Create a CREATE statement

( fooobar.com/questions/82030/...) CREATE , 'from_'.


select  'create table [' + so.name + '] (' + o.list + ')' + CASE WHEN tc.Constraint_Name IS NULL THEN '' ELSE 'ALTER TABLE ' + so.Name + ' ADD CONSTRAINT ' + tc.Constraint_Name  + ' PRIMARY KEY ' + ' (' + LEFT(j.List, Len(j.List)-1) + ')' END as query
OBJECTPROPERTY(object_id(TABLE_NAME), 'TableHasIdentity') as tablehasidentity
from    sysobjects so
cross apply
    (SELECT 
        '  ['+column_name+'] ' + 
        data_type + case data_type
            when 'sql_variant' then ''
            when 'text' then ''
            when 'ntext' then ''
            when 'decimal' then '(' + cast(numeric_precision as varchar) + ', ' + cast(numeric_scale as varchar) + ')'
            else coalesce('('+case when character_maximum_length = -1 then 'MAX' else cast(character_maximum_length as varchar) end +')','') end + ' ' +
        case when exists ( 
        select id from syscolumns
        where object_name(id)=so.name
        and name=column_name
        and columnproperty(id,name,'IsIdentity') = 1 
        ) then
        'IDENTITY(' + 
        cast(ident_seed(so.name) as varchar) + ',' + 
        cast(ident_incr(so.name) as varchar) + ')'
        else ''
        end + ' ' +
         (case when IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' + 
          case when information_schema.columns.COLUMN_DEFAULT IS NOT NULL THEN 'DEFAULT '+ information_schema.columns.COLUMN_DEFAULT ELSE '' END + ', ' 

     from information_schema.columns where table_name = so.name
     order by ordinal_position
    FOR XML PATH('')) o (list)
left join
    information_schema.table_constraints tc
on  tc.Table_name       = so.Name
AND tc.Constraint_Type  = 'PRIMARY KEY'
cross apply
    (select '[' + Column_Name + '], '
     FROM   information_schema.key_column_usage kcu
     WHERE  kcu.Constraint_Name = tc.Constraint_Name
     ORDER BY
        ORDINAL_POSITION
     FOR XML PATH('')) j (list)
where   xtype = 'U'
AND name    NOT IN ('dtproperties') AND name like 'from_%'

:

['query'] = create table [from_users_roles] (  [uid] int  NOT NULL DEFAULT ((0)),   [rid] int  NOT NULL DEFAULT ((0)), )ALTER TABLE from_users_roles ADD CONSTRAINT from_users_roles_pkey PRIMARY KEY  ([uid], [rid])
['tablehasidentity'] = 1 or 0

'from_' 'to_' CREATE:

create table [to_users_roles] (  [uid] int  NOT NULL DEFAULT ((0)),   [rid] int  NOT NULL DEFAULT ((0)), )ALTER TABLE to_users_roles ADD CONSTRAINT to_users_roles_pkey PRIMARY KEY  ([uid], [rid]);

INSERT

, :

TablehasIdentity == 0

INSERT INTO to_users_roles SELECT * FROM from_users_roles

TablehasIdentity == 1

. , IDENTITY_INSERT .

DECLARE @Query nvarchar(4000)
DECLARE @columnlist nvarchar(4000)

// Result of this query e.g.: "[cid], [pid], [nid], [uid], [subject]"
SET @columnlist = (SELECT SUBSTRING((SELECT ', ' + QUOTENAME(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS  WHERE TABLE_NAME = 'from_users_roles' ORDER BY ORDINAL_POSITION FOR XML path('')), 3,  200000))

SET @query ='SET IDENTITY_INSERT to_users_roles ON; INSERT INTO to_users_roles (' + @columnlist + ')  SELECT  ' + @columnlist + ' FROM from_users_roles; SET IDENTITY_INSERT to_users_roles OFF'
exec sp_executesql @query;

.

+5

, , . .

+1

SQL DB V12. .

+1

"select into" "insert select".

. SQL Management Studio "Script " → " " → " ".

. , , "Entities_2015_08_24" ( "Entities" ):

CREATE TABLE [dbo].[Entities_2015_08_24](
    [Url] [nvarchar](max) NULL,
    [ClientID] [nvarchar](max) NULL
)

"insert select" (Entities) (Entities_2015_08_24):

INSERT INTO [dbo].[Entities_2015_08_24]
           ([Url]
           ,[ClientID]
           )
SELECT 
       [Url]
      ,[ClientID]
  FROM [dbo].[Entities]
0

Q: ?
: SQL Azure

AFAIK, you cannot use the syntax select intoto "clone" a table in Azure SQL. Because Azure requires a clustered index, it select intodoes not have the ability to define it.

Details and possible workaround:

http://blogs.msdn.com/b/windowsazure/archive/2010/05/04/select-into-with-sql-azure.aspx

-1
source

All Articles