Create table (structure) from existing table

后端 未结 15 1294
遇见更好的自我
遇见更好的自我 2020-11-27 13:46

How to create new table which structure should be same as another table

I tried

CREATE TABLE dom AS SELECT * FROM dom1 WHERE 1=2

bu

相关标签:
15条回答
  • 2020-11-27 14:05

    Found here what I was looking for. Helped me recall what I used 3-4 years back.

    I wanted to reuse the same syntax to be able to create table with data resulting from the join of a table.

    Came up with below query after a few attempts.

    SELECT a.*
    INTO   DetailsArchive
    FROM   (SELECT d.*
            FROM   details AS d
                   INNER JOIN
                   port AS p
                   ON p.importid = d.importid
            WHERE  p.status = 2) AS a;
    
    0 讨论(0)
  • 2020-11-27 14:10

    Try:

    Select * Into <DestinationTableName> From <SourceTableName> Where 1 = 2
    

    Note that this will not copy indexes, keys, etc.

    If you want to copy the entire structure, you need to generate a Create Script of the table. You can use that script to create a new table with the same structure. You can then also dump the data into the new table if you need to.

    If you are using Enterprise Manager, just right-click the table and select copy to generate a Create Script.

    0 讨论(0)
  • 2020-11-27 14:12
    1. If you Want to copy Same DataBase

      Select * INTO NewTableName from OldTableName
      
    2. If Another DataBase

      Select * INTO NewTableName from DatabaseName.OldTableName
      
    0 讨论(0)
  • 2020-11-27 14:13

    try this.. the below one copy the entire structure of the existing table but not the data.

    create table AT_QUOTE_CART as select * from QUOTE_CART where 0=1 ;
    

    if you want to copy the data then use the below one:

    create table AT_QUOTE_CART as select * from QUOTE_CART ;
    
    0 讨论(0)
  • 2020-11-27 14:13
    SELECT * 
    INTO NewTable
    FROM OldTable
    WHERE 1 = 2
    
    0 讨论(0)
  • 2020-11-27 14:14

    This is what I use to clone a table structure (columns only)...

    SELECT TOP 0 *
    INTO NewTable
    FROM TableStructureIWishToClone
    
    0 讨论(0)
提交回复
热议问题