0% found this document useful (0 votes)
32 views

Create Table

The document describes different SQL methods for inserting data from one table to another. Method 1 uses INSERT INTO SELECT to insert data from an existing source table into an already created target table. Method 2 uses SELECT INTO to both create a new target table and insert data into it from a source table in one statement. The target table will have the same structure as the columns selected from the source table. Additional methods covered are copying a table's structure without data using WHERE 1=2, renaming tables and columns using sp_RENAME, and copying data between databases using SELECT INTO and specifying the target database.

Uploaded by

avantika10
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Create Table

The document describes different SQL methods for inserting data from one table to another. Method 1 uses INSERT INTO SELECT to insert data from an existing source table into an already created target table. Method 2 uses SELECT INTO to both create a new target table and insert data into it from a source table in one statement. The target table will have the same structure as the columns selected from the source table. Additional methods covered are copying a table's structure without data using WHERE 1=2, renaming tables and columns using sp_RENAME, and copying data between databases using SELECT INTO and specifying the target database.

Uploaded by

avantika10
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

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 same, they are are not required to list them. I always list them for readability and scalability purpose.

USE AdventureWorks GO ----Create TestTable CREATE TABLE TestTable (FirstName VARCHAR(100), LastName VARCHAR(100)) ----INSERT INTO TestTable using SELECT INSERT INTO TestTable (FirstName, LastName) SELECT FirstName, LastName FROM Person.Contact WHERE EmailPromotion = 2 ----Verify that Data in TestTable SELECT FirstName, LastName FROM TestTable ----Clean Up Database DROP TABLE TestTable GO 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. USE AdventureWorks GO ----Create new table and insert into table using SELECT INSERT SELECT FirstName, LastName INTO TestTable FROM Person.Contact WHERE EmailPromotion = 2 ----Verify that Data in TestTable SELECT FirstName, LastName FROM TestTable ----Clean Up Database DROP TABLE TestTable GO

Copy only the structure of an existing table into new table: SELECT * INTO tblNew FROM tblOld WHERE 1=2 The above query will copy the structure of an existing table(tblOld) into the new table(tblNew). Copy the structure with data of an existing table into new table: SELECT * INTO tblNew FROM tblOld This is also same like the previous query, but it copies the structure of existing table(tblOld) with data as well into the new table(tblNew).

Rename table name:


sp_RENAME 'Table_First', 'Table_Last'

Rename col name:


sp_RENAME 'Table_First.Name', 'NameChange' , 'COLUMN' To copy from one database to another

SELECT * INTO ravitest.dbo.emp FROM RaviData.dbo.Employee;

You might also like