SELECT INTO – Copy Only Table Structure, Table with Data, Data in SQL Server 2005
SELECT INTO SQL Command allows to copy Table Structure, Rows from an existing Table. If you are not familiar with this Command yet, go through the post. It will give you a good introduction to SELECT INTO Command in SQL Server.
SELECT INTO Syntax:
SELECT select_list [ INTO new_table ] [ FROM table_source ] [ WHERE search_condition ] [ GROUP BY group_by_expression ] [ HAVING search_condition ] [ ORDER BY order_expression [ ASC | DESC ] ]
Let's jump in to coding directly.
-- Create Table CREATE TABLE Table_Customer ( FirstName nvarchar(50) NULL, LastName nvarchar(50) NULL, City nvarchar(50) NULL ) -- Let's Insert some Demo Data INSERT INTO Table_Customer VALUES ('A Rahim','Khan','Dhaka') INSERT INTO Table_Customer VALUES ('A Karim','Khan','Kurigram') -- To copy table with data SELECT FirstName, LastName, City INTO Table_CustomerCopyWithData FROM Table_Customer -- Just to copy table structure SELECT FirstName, LastName, City INTO Table_CustomerCopyWithOutData FROM Table_Customer WHERE 2=1 -- You can even set data selection criteria to copy data with table structure SELECT FirstName, LastName, City INTO Table_CustomerCopyWithFilteredData FROM Table_Customer WHERE City = 'Dhaka' -- To insert data from one table to another INSERT INTO Table_CustomerCopyWithOutData SELECT * FROM Table_Customer
A Rahim Khan