Thursday, October 31, 2013

How to CREATE a Copy or Duplicate Table of another Table in SQL Server

SQL Query to Copy a Table Structure from another Table
Method 1 : INSERT INTO SELECT
This method is used when table is already created in the database earlier and data is to be inserted into this table from another table. If columns listed in insert clause and select clause are of same data type, they are are not required to list them. I always list them for readability and scalability purpose.

Copying Specific subset of columns into an Existing Table from another Existing Table : 

CREATE TABLE SampleTable(PID Int,FirstName VARCHAR(100), LastNameVARCHAR(100))
INSERT INTO 
SampleTable(PID,FirstName, LastName) SELECT PNo FName, LName FROM Persons. 


Notes :
Here the columns names may different but the data type should be same.

Copying all columns into an Existing Table from another Existing Table : 
To do this both tables should have same no.of columns and same data type.
INSERT INTO SampleTable SELECT * FROM Persons. 

Method 2 : SELECT INTO :
This method is used when table is not created earlier and needs to be created when data from one table is to be inserted into newly created table from another table. New table is created with same data types as selected columns.
The easiest way to create a copy of a table is to use a Transact-SQL command SELECT INTO to extract all the rows from an existing table into the new table. The new table must not exist already.
The following example will copy the Customers table under the Sales Schema to a new table called NewCustomers under the Orders Schema:
SELECT * INTO Orders.NewCustomers FROM Sales.Customers;

We can create a Table from an existing Table by copying the existing Table's Columns.
It is important to note that when creating a Table in this way, the New Table will be populated with the records from the existing Table (based on the SQL SELECT Statement).

Copying Specific subset of columns from Existing Table to New Table :
You can also create the new table from a specific subset of columns in the original table. In this case, you specify the names of the columns to copy after the SELECT keyword. Any columns not specified are excluded from the new table. The following example copies specific columns to a new table:

SELECT CustName, Address, Telephone, Email INTO Orders.NewCustomers
FROM Sales.Customers;

Copying only Table Structure from Existing Table to New Table :
Creating a duplicate table with only the structure duplicated with a new name as follows :
SELECT * INTO TPR.NewCustomers FROM Sales.Customers where 1=2;

Oracle/PL-SQL :
Copying all Columns from another Table :
The following query will create a New Table called Vendors that included all Columns from the Customers Table.
If there were records in the Customers Table, then the New Vendors Table would also contain the records selected by the SELECT statement.
CREATE TABLE Vendors
AS (
SELECT *
FROM Customers
WHERE id > 1000
);

Copying selected Columns from another Table :
The following query will create a New Table called Vendors, but the New Table would only include the specified Columns from the Customers Table.
Again, if there were records in the Customers Table, then the New Vendors Table would also contain the records selected by the SELECT statement.

CREATE TABLE Vendors
AS (
SELECT id, address, city, state, zip
FROM Customers
WHERE id > 1000
);

Copying selected Columns from multiple Tables :
The following query will create a New Table called Vendors based on Columns from both the Customers and Categories Tables.
CREATE TABLE Vendors
AS (
SELECT Customers.id, Customers.address, Categories.cat_type
FROM Customers, Categories
WHERE Customers.id = Categories.id
AND Customers.id > 1000
);

Creating a SQL Table from another Table without copying values but the Structure :
The following query will create a New Table called Vendors that included all Columns from the Customers Table, but no data from the Customers Table
CREATE TABLE Vendors
AS (SELECT * FROM Customers WHERE 1=2);


--------------------------------------------------------------------------------------------------------
Thanks, TAMATAM ; Business Intelligence & Analytics Professional
--------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog