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
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;
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.
If you Want to copy Same DataBase
Select * INTO NewTableName from OldTableName
If Another DataBase
Select * INTO NewTableName from DatabaseName.OldTableName
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 ;
SELECT *
INTO NewTable
FROM OldTable
WHERE 1 = 2
This is what I use to clone a table structure (columns only)...
SELECT TOP 0 *
INTO NewTable
FROM TableStructureIWishToClone