Pages

6 August 2013

SQL- Copying Structure of Table without copying Data

Q. How to copy the structure of a table without copying the data ?
           It is possible to copy the structure of a table without copying the data. The following below query will help you in achieving this.

Syntax:
SELECT * INTO <TargetTableName> FROM <SourceTableName> WHERE 1=2

 In the below snapshot Customer is a source table from where we are copying the
structure of a table to a target table XYZ.


Copying structure of a table 1.1
     The following below snapshot verifies that Customer's table structure has been copied to XYZ table.

Structure gets copied

   Suppose if you want to copy the data along with structure, you just need to drop the WHERE clause in above query. 

SELECT * INTO <TargetTableName> FROM <SourceTableName>

NOTE: While copying the structure of a table, it does not copy KEYS, INDEXES, Constraints etc.