Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Blogs Technet Com B Mdegre Archive 2009-09-04 SQL Server How

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Sign in

TechNet Blogs > Microsoft SQL Server > How to insert data from one table to another table

Common Tasks
Blog Home Email Blog Author About RSS for comments RSS for posts

How to insert data from one table to another table


Degremont Michel 4 Sep 2009 3:45 PM

RATE THIS

SQL globe readers


How to insert data from one table to another table by using SQL Server There are various ways to insert data from one table to another table. I will present you the most efficently. Using INSERT INTO This method is used when the table is already existing in the database. The datas will be inserted into this table from another table. You don't have to list the columns If columns listed in insert clause and select clause are egual and with the same type. But to avoid any mistake, I recommand you to list each column. You can create an Insert From query, when you specify: - The database table to copy rows to (the destination table). - The table or tables to copy rows from (the source table). - The source table or tables become part of a subquery. - Sort order, if you want to copy the rows in a particular order. - Group By options, if you want to copy only summary information. Sample:

Search
Search TechNet with Bing Search this blog Search all blogs

Recent Posts
Command sp_cycle_agent_errorlog is failing Unable to install SCOM 2012 Reporting Services How configure correctly AlwaysOn readable secondary replica for applicationIntent How to create a dependency between
Are you a developer? Try out the HTML to PDF API

open in browser PRO version

pdfcrowd.com

CREATE TABLE [tempdb].[dbo].[myTempTable]( [backup_set_id] [int] NOT NULL, [name] [nvarchar](128) NOT NULL, [filegroup_id] [int] NOT NULL, [filegroup_guid] [uniqueidentifier] NULL, [type] [char](2) NOT NULL, [type_desc] [nvarchar](60) NOT NULL, [is_default] [bit] NOT NULL, [is_readonly] [bit] NOT NULL, [log_filegroup_guid] [uniqueidentifier] NULL ) ON [PRIMARY] GO INSERT INTO [tempdb].[dbo].[myTempTable] ( [backup_set_id] ,[name] ,[filegroup_id] ,[filegroup_guid] ,[type] ,[type_desc] ,[is_default] ,[is_readonly] ,[log_filegroup_guid]) SELECT [backup_set_id] ,[name] ,[filegroup_id] ,[filegroup_guid] ,[type] ,[type_desc] ,[is_default] ,[is_readonly] ,[log_filegroup_guid] FROM [msdb].[dbo].[backupfilegroup] GO SELECT * FROM [tempdb].[dbo].[myTempTable] ; GO DROP TABLE [tempdb].[dbo].[myTempTable] ;

How to create a dependency between two or more Availability Groups AlwaysOn warning: The current WSFC cluster quorum vote configuration is not recommended for this availablity group

Tags
Alw aysOn Av ailability Group

AMO

Analysis Services

Cluster
TIP

Connectiv ity

DBA

DC High Availability

Installation
MSCS Performance

Kerberos

Management Studio Migration

Powershell

Replication

Security

SQL

Server

SQL Serv er Clustering

SQL Server High Availability


SSMS

SSAS

SSIS

T-SQL

Windlows

Cluster
Archives
September 2013 (1) May 2013 (1) April 2013 (2) March 2013 (2) December 2012 (1)

Inserting Rows by Using SELECT INTO

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

With this method you don't need to create the table before the insert. Table will be created when data from one table will be inserted from another table. This new table will be created with same data types as selected columns. SELECT INTO can be used to combine data from several tables or views into one table. It can also be used to create a new table that contains data selected from a linked server. Sample:

December 2012 (1) October 2012 (1) September 2012 (1) August 2012 (1) June 2012 (3) April 2012 (6) March 2012 (6) February 2012 (9) January 2012 (3) December 2011 (3) November 2011 (6) October 2011 (7) September 2011 (6) July 2011 (9) June 2011 (6) March 2011 (3) January 2011 (4) December 2010 (3) November 2010 (3) September 2010 (6) August 2010 (6) July 2010 (3) June 2010 (3) May 2010 (2) March 2010 (4) February 2010 (4) January 2010 (2) December 2009 (4) November 2009 (2) October 2009 (4) September 2009 (4) August 2009 (4) July 2009 (4)

SELECT [backup_set_id] ,[name] ,[filegroup_id] ,[filegroup_guid] ,[type] ,[type_desc] ,[is_default] ,[is_readonly] ,[log_filegroup_guid] INTO [tempdb].[dbo].[myTempTable] FROM [msdb].[dbo].[backupfilegroup] GO SELECT * FROM [tempdb].[dbo].[myTempTable] ; GO DROP TABLE [tempdb].[dbo].[myTempTable] ;
SELECT INTO is non-logged operation when the db is in bulk_logged mode.You can use BCP too... Read the the following article for more info. http://support.microsoft.com/?scid=kb;en-us;Q272093 INSERT INTO is recommended over SELECT INTO since the later requires more lock resources.

Don't ask you which one is the best. You have to keep in mine that this both solution are different and adapted to a specific value.

Michel Degremont | Microsoft EMEA

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Product Support Services Developer - SQL Server Core Engineer |

SQL Server, T-SQL

Leave a Comment
Name

Comment

Post

Comments
Torsten Hi, how do you specify the sort order, as mentioned "- Sort order, if you want to copy the rows in a particular order." INSERT INTO [tempdb].[dbo].[myTempTable] ( ..) SELECT ... FROM [msdb].[dbo].[backupfilegroup] ORDER BY [column1] Won't change anything. 7 Feb 2013 7:47 AM

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

I need this, because my table has foreign key relations on itself (col1=col2). so some rows need to be inserted before others.

Degremont Michel Hi,

20 Mar 2013 4:40 AM

you just have to add the ORDER BY clause after SELECT * FROM [tempdb].[dbo].[myTempTable] ; and you can chose by with columns. Let me know if you need any help.

2013 Microsoft Corporation. Term s of Use

Tradem arks

Privacy & Cookies

5.6.426.415

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

You might also like