How to generate scripts to recreate table using SQL Server Management Studio [Schema and data]?

Sql ServerSsmsSql Server-2008

Sql Server Problem Overview


I have a table in a local SQL server database. I want to recreate this table in a hosted database.

What I want to do is to have a script that when run against the hosted database, this table is recreated with all the data, etc.

How do I create this script using SQL Server Management Studio? Thanks.

Sql Server Solutions


Solution 1 - Sql Server

1- Open SQL server Management Studio.

2- Right click on the DB that contains your desired table.

3- Select "Tasks => Generate Scripts...".

4- Follow on the wizard, and choose the objects that you want to generate scripts for (Tables, Views, Stored Procedures, etc... ).

5- From the next step, click on "Advanced", and for the node that is labeled "Types of data to script" choose "Schema and data".

enter image description here

6- Save your script and smile :)

Solution 2 - Sql Server

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')

Solution 3 - Sql Server

You can do this using Generate Scripts database task. Right-click the database > Tasks > Generate Scripts... Choose "Select specific database objects" and the table you want.

On the Set Scripting Options page, click Advanced. There is an option "Types of data to script", choose "Schema and data". Choose where to save it. Next. Next. Finish.

That is the answer however, if the table contains a large amount of data I recommend using bcp out or another method to export the data. If the new server is on the same network, you could also select it as a linked server.

The script method will generate individual insert statements.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser763554View Question on Stackoverflow
Solution 1 - Sql ServerMohammed SwillamView Answer on Stackoverflow
Solution 2 - Sql ServerIR.ProgrammerView Answer on Stackoverflow
Solution 3 - Sql ServerRussell HartView Answer on Stackoverflow