2012-11-22 4 views

risposta

85

1- Aprire SQL Management Studio server.

2- Fare clic con il pulsante destro del mouse sul DB che contiene la tabella desiderata.

3- Seleziona "Attività => Genera script ...".

4- Seguire la procedura guidata e scegliere gli oggetti per cui si desidera generare script (tabelle, viste, stored procedure, ecc.).

5- Dalla fase successiva, cliccare su "Avanzate ", e per il nodo che viene etichettato come "tipi di dati da script" scegliere "schema e dati".

enter image description here

6- Salvare il copione e sorriso :)

3

È possibile farlo usando Genera script compito di database. Fare clic con il tasto destro del mouse sul database> Attività> Genera script ... Scegliere "Seleziona oggetti database specifici" e la tabella desiderata.

Nella pagina Imposta opzioni di script, fare clic su Avanzate. Esiste un'opzione "Tipi di dati da script", scegliere "Schema e dati". Scegli dove salvarlo. Il prossimo. Il prossimo. Finire.

Questa è la risposta, tuttavia, se la tabella contiene una grande quantità di dati, consiglio di utilizzare bcp out o un altro metodo per esportare i dati. Se il nuovo server si trova sulla stessa rete, è possibile selezionarlo anche come server collegato.

Il metodo di script genererà singole istruzioni di inserimento.

4
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 
from sysobjects so 
cross apply 
    (SELECT 
     ' ['+column_name+'] ' + 
     data_type + case data_type 
      when 'sql_variant' then '' 
      when 'text' then '' 
      when 'ntext' then '' 
      when 'xml' 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')