Pages

Monday, March 30, 2009

Create Table Using Select in Sql Server

In this section i would like to give you a small tip about creating a table in SQL Server with out using CREATE TABLE statement.


SELECT * INTO EMPLOYEE_WORKING_HRS_STG
FROM EMPLOYEE_WORKING_HRS

If you look at the query I am trying to create a replica of the EMPLOYEE_WORKING_HRS table with a new name. Or you can even say I am creating a new table with the same structure as EMPLOYEE_WORKING_HRS and then inserting data into it.

Now let's say you don't want to copy the data, just you want to create another table then execute the below query.


SELECT * INTO EMPLOYEE_WORKING_HRS_STG
FROM EMPLOYEE_WORKING_HRS
where 1=2



The only difference between this and the previous one is WHERE condition.

2 comments:

Fazalhusein Z Patvi said...

Thanks. it helped me create a table.

Anonymous said...

Thanks, VJ.

A reminder to users -- this does not copy keys or indices.

JenisysJohn