1

別のテーブルに同じ構造のテーブルを作成したい。

ただし、その古いテーブルのすべての主キーとインデックスを使用して同じテーブル構造を作成する必要があります。

以下を試しましたが、列のみがコピーされます。主キーもインデックスもコピーされません。

SELECT *
INTO dbo.NewTable
FROM dbo.ExistingTable
WHERE 1 = 2

どうすればそれをコピー/クローンできますか?

4

1 に答える 1

1

データベースのクローンを作成するために使用したスクリプトの一部を投稿しています。一部を削除しましたが、FKとインデックスがありません(ただし、PKはあり、IDを無視します:P)が、どのように行うかについてのヒントが得られるはずです。それを行うことができます

注意:スケジュールどおりではなく1回実行する必要があるため、最適化されていません。データベースで機能するようにしました:Pニーズに合わせて修正してください:

declare @DestinationSchema nvarchar(50) = 'frontier'
declare @SourceSchema nvarchar(50) = 'dbo'

select  so.name as TableName
        ,N'create table [' + @DestinationSchema + '].[' + so.name + '] (' + o.list + (case when kc.name IS NULL then '' else ' CONSTRAINT ' + kc.name  + ' PRIMARY KEY CLUSTERED ' + ' (' + LEFT(j.List, Len(j.List)-1) + '))' end) as TableScript
from    sys.objects so
cross apply
    (select 
        -- column name
        '  ['+cl.name+'] ' + 
        -- column type
        t.name + 
        -- type lenght
        (case t.name
            when 'sql_variant' then ''
            when 'text' then ''
            when 'ntext' then ''
            when 'bit' then '' 
            when 'int' then ''
            when 'tinyint' then ''
            when 'smallint' then ''
            when 'bigint' then ''
            when 'timestamp' then ''
            when 'date' then ''
            when 'smalldatetime' then ''
            when 'datetime' then ''
            when 'datetime2' then ''
            when 'real' then ''
            when 'float' then ''
            when 'time' then ''
            when 'decimal' then '(' + cast(cl.[precision] as varchar) + ', ' + cast(cl.scale as varchar) + ')'
            else '('+ (case when cl.max_length = -1 then 'MAX' else cast(cl.max_length as varchar) end) +')' end) + ' ' +
        -- nullable
        (case when cl.is_nullable = 0 then 'NOT ' else '' end ) + 'NULL,'
     from sys.columns cl
            inner join sys.types t on cl.user_type_id = t.user_type_id
     where object_id = so.object_id
     order by cl.column_id
    for xml path('')
    ) o (list)
inner join sys.schemas sch on so.[schema_id] = sch.[schema_id] AND sch.name = @SourceSchema
left join sys.key_constraints kc on so.[object_id] = kc.parent_object_id AND kc.[type] = 'PK' AND kc.[schema_id] = so.[schema_id]
cross apply
    (select N'[' + col.name + '], '
     from   sys.columns col 
                inner join sys.indexes i on col.[object_id] = i.[object_id] and i.is_primary_key = 1
                inner join sys.index_columns ic on ic.object_id = so.object_id and ic.column_id = col.column_id and ic.index_id = i.index_id
     where  col.[object_id] = so.[object_id]
     order by ic.key_ordinal
     for xml path('')) j (list)
cross apply
    (select N'[Destination].[' + col.name + '] = [Source].[' + col.name + '] AND '
     from   sys.columns col 
                inner join sys.indexes i on col.[object_id] = i.[object_id] and i.is_primary_key = 1
                inner join sys.index_columns ic on ic.object_id = so.object_id and ic.column_id = col.column_id and ic.index_id = i.index_id
     where  col.[object_id] = so.[object_id]
     order by ic.key_ordinal
     for xml path('')) k (list)     
where
    so.[type] = 'U'
order by
    so.name
于 2012-11-19T10:21:49.640 に答える