How do I create a duplicate table with only the structure duplicated with a new name in SQL server 2008?
I have table with 45 fields so I want to create new with same structure but new name.
How do I create a duplicate table with only the structure duplicated with a new name in SQL server 2008?
I have table with 45 fields so I want to create new with same structure but new name.
SELECT * INTO newtable FROM oldtable where 1=2
Where 1=2 is used when you need to copy the complete structure of a
table without copying the data.
SELECT * INTO newtable FROM oldtable
To create a table with data you can use this statement.
I have used this query it is created new table with existing data.
Query : select * into [newtablename] from [existingtable]
Here is the link Microsoft instructions. https://docs.microsoft.com/en-us/sql/relational-databases/tables/duplicate-tables?view=sql-server-2017
In SSMS right click on a desired table > script as > create to > new query
-change the name of the table (ex. table2)
-change the PK key for the table (ex. PK_table2)
USE [NAMEDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[table_2](
[id] [int] NOT NULL,
[name] [varchar](50) NULL,
CONSTRAINT [PK_table_2] PRIMARY KEY CLUSTERED
(
[reference] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE =
OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
Duplication table in the SQL means creating the new table with a same column name from the existing old table which are duplicated. Let us take the table consists of more than 50 fields and you want to create a table with exactly same column but in another new table. If we start creating table from the scratch, it would take more time and to avoid this table duplication can be done which creates a new table with same fields. For example let us table this sample table, Tbldepartment which contains following fields,
If we want to duplicate the table,
SELECT *
INTO [NewTbl_Apartments]
FROM [Tbl_Apartments]
WHERE 1 = 2
Here where is mentioned as 1=2, this will prevent data coping from the existing table into newly created duplicate table.