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

2

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

[type_desc] [nvarchar](60) NOT NULL.[name] .[log_filegroup_guid]) SELECT [backup_set_id] .[myTempTable] .[is_default] .[log_filegroup_guid] FROM [msdb].[type_desc] .[filegroup_guid] .[dbo].[type_desc] . [type] [char](2) NOT NULL.[myTempTable]( [backup_set_id] [int] NOT NULL. GO DROP TABLE [tempdb].[is_default] . [is_default] [bit] NOT NULL.[filegroup_id] . 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.[dbo].com .[type] . [log_filegroup_guid] [uniqueidentifier] NULL ) ON [PRIMARY] GO INSERT INTO [tempdb].[type] .[dbo]. [name] [nvarchar](128) NOT NULL.[is_readonly] . [filegroup_id] [int] NOT NULL.[is_readonly] .[myTempTable] .[name] . [is_readonly] [bit] NOT NULL.[backupfilegroup] GO SELECT * FROM [tempdb].[dbo].[dbo]. [filegroup_guid] [uniqueidentifier] NULL.CREATE TABLE [tempdb].[myTempTable] ( [backup_set_id] .[filegroup_id] .[filegroup_guid] .

[dbo].com . You have to keep in mine that this both solution are different and adapted to a specific value.en-us.[is_default] .[log_filegroup_guid] INTO [tempdb].[filegroup_guid] .[dbo].[myTempTable] .microsoft.[type] .com/?scid=kb. Read the the following article for more info..[backupfilegroup] GO SELECT * FROM [tempdb]. This new table will be created with same data types as selected columns..[type_desc] . 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] .With this method you don't need to create the table before the insert. SELECT INTO is non-logged operation when the db is in bulk_logged mode.[dbo].[dbo].[name] . http://support. SELECT INTO can be used to combine data from several tables or views into one table.You can use BCP too. Table will be created when data from one table will be inserted from another table. It can also be used to create a new table that contains data selected from a linked server. GO DROP TABLE [tempdb].[myTempTable] . Michel Degremont | Microsoft EMEA open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.Q272093 INSERT INTO is recommended over SELECT INTO since the later requires more lock resources.[filegroup_id] .[is_readonly] .[myTempTable] FROM [msdb]. Don't ask you which one is the best.

[dbo].com . as mentioned ". how do you specify the sort order.[backupfilegroup] ORDER BY [column1] Won't change anything.. T-SQL Leave a Comment Name Comment Post Comments Torsten Hi.[dbo]. FROM [msdb].SQL Server Core Engineer | SQL Server.) SELECT .Product Support Services Developer . 7 Feb 2013 7:47 AM open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.Sort order. if you want to copy the rows in a particular order..[myTempTable] ( .." INSERT INTO [tempdb].

because my table has foreign key relations on itself (col1=col2).426. Term s of Use Tradem arks Privacy & Cookies 5.com .6. so some rows need to be inserted before others.415 open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.[dbo]. Degremont Michel Hi.I need this.[myTempTable] . and you can chose by with columns. 20 Mar 2013 4:40 AM you just have to add the ORDER BY clause after SELECT * FROM [tempdb]. © 2013 Microsoft Corporation. Let me know if you need any help.