Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8850565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:55:59+00:00 2026-06-14T12:55:59+00:00

I want to create same structure of table in another table.. But it should

  • 0

I want to create same structure of table in another table..

But it should create same table structure with all primary keys and indexes of that old table.

I tried below but it copies only columns.. not primary key nor indexes.

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

How can I copy/clone that too?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:56:01+00:00Added an answer on June 14, 2026 at 12:56 pm

    i’m posting you a part of a script i used to create a clone of a database, i removed some parts and it’s missing FKs and indexes (but PKs are there, ignoring identity :P) but should give you a hint on how you could do it

    beware: it’s not optimised since i need to run it 1 time and not on a schedule, i just made it work with my databases 😛 fix it for your needs:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would want to create a table with the same widths (table and its
How to select a same database? I want to create a table from already
I want to create login and password fields same as in facebook app for
I want to create a FlipPanel, which provides two different views of the same
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
I have a MySQL Table that looks like this: The SQL to create the
I have a tree structure in an sql table like so: CREATE TABLE containers
How do I create a duplicate table with only the structure duplicated with a
I tried to create a pivot table created from a table in Oracle 10g.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.