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


Thanks
A Rahim Khan

Advertisements
  1. Thanks.

    • abdul
    • September 27th, 2012

    good explanation

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: