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

Embed Size (px)

Citation preview

  • 7/27/2019 Blogs Technet Com b Mdegre Archive 2009-09-04 SQL Server How

    1/5

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Common Tasks

    SQL globe readers

    Search

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

    Blog Home

    Email Blog AuthorAbout

    RSS for comments

    RSS for posts

    Search TechNet with Bing

    Search this blog Search all blogs

    Recent Posts

    Command sp_cycle_agent_errorlog isfailing

    Unable to install SCOM 2012 ReportingServices

    How configure correctly AlwaysOnreadable secondary replica for

    applicationIntent

    RATE THIS

    How to insert data from one table to anothertable

    2Degremont Michel 4 Sep 2009 3:45 PM

    How to insert data from one table to another table by using SQL Server

    There are various ways to ins ert data from one table to another table. I will present you the mos t efficently.

    Using INSERT INTO

    This method is used when the table is already exis ting in the database. The datas will be ins erted into this table from

    another table.

    You don't have to list the columns If columns listed in ins ert clause and select clause are egual and with the same

    ype. But to avoid any mistake, I recommand you to lis t 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:

    Sign in

    http://blogs.technet.com/b/mdegre/archive/2013/05/27/unable-to-install-scom-2012-reporting-services.aspxhttp://blogs.technet.com/b/mdegre/rss.aspxhttp://blogs.technet.com/b/mdegre/contact.aspxhttp://blogs.technet.com/b/mdegre/archive/2013/09/03/command-sp-cycle-agent-errorlog-is-failing.aspxhttp://blogs.technet.com/b/mdegre/contact.aspxhttps://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1379661751&rver=6.0.5286.0&wp=MBI&wreply=http:%2F%2Fblogs.technet.com%2Fb%2Fmdegre%2Farchive%2F2009%2F09%2F04%2Fsql-server-how-to-insert-data-from-one-table-to-another-table.aspx&lc=1033&id=271612http://blogs.technet.com/b/mdegre/archive/2013/09/03/command-sp-cycle-agent-errorlog-is-failing.aspxhttp://blogs.technet.com/mdegre/archive/2009/09/08/sql-server-how-to-insert-data-from-one-table-to-another-table.aspx?frhttp://blogs.technet.com/mdegre/archive/2009/09/08/sql-server-how-to-insert-data-from-one-table-to-another-table.aspx?eshttp://blogs.technet.com/mdegre/archive/2009/09/08/sql-server-how-to-insert-data-from-one-table-to-another-table.aspx?ushttp://blogs.technet.com/68307/ProfileUrlRedirect.ashxhttp://blogs.technet.com/68307/ProfileUrlRedirect.ashxhttp://blogs.technet.com/b/mdegre/rss.aspxhttp://blogs.technet.com/b/mdegre/contact.aspxhttp://blogs.technet.com/http://blogs.technet.com/b/mdegre/http://blogs.technet.com/b/mdegre/archive/2009/09/04/sql-server-how-to-insert-data-from-one-table-to-another-table.aspxhttp://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fblogs.technet.com%2fb%2fmdegre%2farchive%2f2009%2f09%2f04%2fsql-server-how-to-insert-data-from-one-table-to-another-table.aspx&id=ma-130920032234-d760c03dhttp://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://blogs.technet.com/http://blogs.technet.com/b/mdegre/http://blogs.technet.com/b/mdegre/archive/2009/09/04/sql-server-how-to-insert-data-from-one-table-to-another-table.aspxhttp://blogs.technet.com/b/mdegre/http://blogs.technet.com/b/mdegre/contact.aspxhttp://blogs.technet.com/b/mdegre/about.aspxhttp://blogs.technet.com/b/mdegre/rsscomments.aspxhttp://blogs.technet.com/b/mdegre/rss.aspxhttp://www3.clustrmaps.com/counter/maps.php?url=http://blogs.technet.com/mdegre/http://blogs.technet.com/b/mdegre/archive/2013/09/03/command-sp-cycle-agent-errorlog-is-failing.aspxhttp://blogs.technet.com/b/mdegre/archive/2013/05/27/unable-to-install-scom-2012-reporting-services.aspxhttp://blogs.technet.com/b/mdegre/archive/2013/04/12/read-only-routing-is-not-working-correctly.aspxhttp://blogs.technet.com/b/mdegre/archive/2013/04/08/how-to-create-a-dependency-between-two-or-more-availability-groups.aspxhttp://blogs.technet.com/68307/ProfileUrlRedirect.ashxhttp://blogs.technet.com/68307/ProfileUrlRedirect.ashxhttp://blogs.technet.com/mdegre/archive/2009/09/08/sql-server-how-to-insert-data-from-one-table-to-another-table.aspx?frhttp://blogs.technet.com/mdegre/archive/2009/09/08/sql-server-how-to-insert-data-from-one-table-to-another-table.aspx?eshttp://blogs.technet.com/mdegre/archive/2009/09/08/sql-server-how-to-insert-data-from-one-table-to-another-table.aspx?ushttps://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1379661751&rver=6.0.5286.0&wp=MBI&wreply=http:%2F%2Fblogs.technet.com%2Fb%2Fmdegre%2Farchive%2F2009%2F09%2F04%2Fsql-server-how-to-insert-data-from-one-table-to-another-table.aspx&lc=1033&id=271612
  • 7/27/2019 Blogs Technet Com b Mdegre Archive 2009-09-04 SQL Server How

    2/5

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Tags

    AlwaysOn Av ailability Group AMO

    Analysis Services

    Cluster Connectiv ity DBA

    TIP DC High Availability

    Installation Kerberos

    Management Studio Migration

    MSCS Performance Powershell

    Replication Security SQL

    Server SQL Serv er Clustering

    SQL Server High

    Availability SSAS SSIS

    SSMS T-SQL Windlows

    Cluster

    Archives

    September 2013 (1)

    May 2013 (1)

    April 2013 (2)

    March 2013 (2)

    How to create a dependency between

    two or more Availability Groups

    AlwaysOn warning: The current WSFCcluster quorum vote configuration is notrecommended for this availablity group

    CREATETABLE [tempdb].[dbo].[myTempTable](

    [backup_set_id] [int] NOTNULL,

    [name] [nvarchar](128)NOTNULL,

    [filegroup_id] [int] NOTNULL,[filegroup_guid] [uniqueidentifier] NULL,

    [type] [char](2)NOTNULL,

    [type_desc] [nvarchar](60)NOTNULL,

    [is_default] [bit] NOTNULL,[is_readonly] [bit] NOTNULL,

    [log_filegroup_guid] [uniqueidentifier] NULL

    )ON [PRIMARY]GO

    INSERTINTO [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]GOSELECT*FROM [tempdb].[dbo].[myTempTable] ;

    GO

    DROPTABLE [tempdb].[dbo].[myTempTable] ;

    Inserting Rows by Using SELECT INTO

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fblogs.technet.com%2fb%2fmdegre%2farchive%2f2009%2f09%2f04%2fsql-server-how-to-insert-data-from-one-table-to-another-table.aspx&id=ma-130920032234-d760c03dhttp://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://blogs.technet.com/b/mdegre/archive/tags/AlwaysOn+Availability+Group/http://blogs.technet.com/b/mdegre/archive/tags/AMO/http://blogs.technet.com/b/mdegre/archive/tags/Analysis+Services/http://blogs.technet.com/b/mdegre/archive/tags/Cluster/http://blogs.technet.com/b/mdegre/archive/tags/Connectivity/http://blogs.technet.com/b/mdegre/archive/tags/DBA+TIP/http://blogs.technet.com/b/mdegre/archive/tags/DC/http://blogs.technet.com/b/mdegre/archive/tags/High+Availability/http://blogs.technet.com/b/mdegre/archive/tags/Installation/http://blogs.technet.com/b/mdegre/archive/tags/Kerberos/http://blogs.technet.com/b/mdegre/archive/tags/Management+Studio+/http://blogs.technet.com/b/mdegre/archive/tags/Migration/http://blogs.technet.com/b/mdegre/archive/tags/MSCS/http://blogs.technet.com/b/mdegre/archive/tags/Performance/http://blogs.technet.com/b/mdegre/archive/tags/Powershell/http://blogs.technet.com/b/mdegre/archive/tags/Replication/http://blogs.technet.com/b/mdegre/archive/tags/Security/http://blogs.technet.com/b/mdegre/archive/tags/SQL+Server/http://blogs.technet.com/b/mdegre/archive/tags/SQL+Server+Clustering/http://blogs.technet.com/b/mdegre/archive/tags/SQL+Server+High+Availability/http://blogs.technet.com/b/mdegre/archive/tags/SSAS/http://blogs.technet.com/b/mdegre/archive/tags/SSIS/http://blogs.technet.com/b/mdegre/archive/tags/SSMS/http://blogs.technet.com/b/mdegre/archive/tags/T_2D00_SQL/http://blogs.technet.com/b/mdegre/archive/tags/Windlows+Cluster/http://blogs.technet.com/b/mdegre/archive/2013/09.aspxhttp://blogs.technet.com/b/mdegre/archive/2013/05.aspxhttp://blogs.technet.com/b/mdegre/archive/2013/04.aspxhttp://blogs.technet.com/b/mdegre/archive/2013/03.aspxhttp://blogs.technet.com/b/mdegre/archive/2012/12.aspxhttp://blogs.technet.com/b/mdegre/archive/2013/04/08/how-to-create-a-dependency-between-two-or-more-availability-groups.aspxhttp://blogs.technet.com/b/mdegre/archive/2013/03/29/alwayson-warning-the-current-wsfc-cluster-quorum-vote-configuration-is-not-recommended-for-this-availablity-group.aspx
  • 7/27/2019 Blogs Technet Com b Mdegre Archive 2009-09-04 SQL Server How

    3/5

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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)

    ith this m ethod you don't need to create the table before the insert. Table will be created when data from one table

    will be ins erted 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 s everal tables or views into one table. It can also be used to create

    a new table that contains data s elected from a linked s erver.

    Sample:

    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

    DROPTABLE [tempdb].[dbo].[myTempTable] ;

    SELECT INTO is non-logged operation when the db is in bulk_logged m ode.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

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fblogs.technet.com%2fb%2fmdegre%2farchive%2f2009%2f09%2f04%2fsql-server-how-to-insert-data-from-one-table-to-another-table.aspx&id=ma-130920032234-d760c03dhttp://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://blogs.technet.com/b/mdegre/archive/2012/12.aspxhttp://blogs.technet.com/b/mdegre/archive/2012/10.aspxhttp://blogs.technet.com/b/mdegre/archive/2012/09.aspxhttp://blogs.technet.com/b/mdegre/archive/2012/08.aspxhttp://blogs.technet.com/b/mdegre/archive/2012/06.aspxhttp://blogs.technet.com/b/mdegre/archive/2012/04.aspxhttp://blogs.technet.com/b/mdegre/archive/2012/03.aspxhttp://blogs.technet.com/b/mdegre/archive/2012/02.aspxhttp://blogs.technet.com/b/mdegre/archive/2012/01.aspxhttp://blogs.technet.com/b/mdegre/archive/2011/12.aspxhttp://blogs.technet.com/b/mdegre/archive/2011/11.aspxhttp://blogs.technet.com/b/mdegre/archive/2011/10.aspxhttp://blogs.technet.com/b/mdegre/archive/2011/09.aspxhttp://blogs.technet.com/b/mdegre/archive/2011/07.aspxhttp://blogs.technet.com/b/mdegre/archive/2011/06.aspxhttp://blogs.technet.com/b/mdegre/archive/2011/03.aspxhttp://blogs.technet.com/b/mdegre/archive/2011/01.aspxhttp://blogs.technet.com/b/mdegre/archive/2010/12.aspxhttp://blogs.technet.com/b/mdegre/archive/2010/11.aspxhttp://blogs.technet.com/b/mdegre/archive/2010/09.aspxhttp://blogs.technet.com/b/mdegre/archive/2010/08.aspxhttp://blogs.technet.com/b/mdegre/archive/2010/07.aspxhttp://blogs.technet.com/b/mdegre/archive/2010/06.aspxhttp://blogs.technet.com/b/mdegre/archive/2010/05.aspxhttp://blogs.technet.com/b/mdegre/archive/2010/03.aspxhttp://blogs.technet.com/b/mdegre/archive/2010/02.aspxhttp://blogs.technet.com/b/mdegre/archive/2010/01.aspxhttp://blogs.technet.com/b/mdegre/archive/2009/12.aspxhttp://blogs.technet.com/b/mdegre/archive/2009/11.aspxhttp://blogs.technet.com/b/mdegre/archive/2009/10.aspxhttp://blogs.technet.com/b/mdegre/archive/2009/09.aspxhttp://blogs.technet.com/b/mdegre/archive/2009/08.aspxhttp://blogs.technet.com/b/mdegre/archive/2009/07.aspxhttp://support.microsoft.com/?scid=kb;en-us;Q272093
  • 7/27/2019 Blogs Technet Com b Mdegre Archive 2009-09-04 SQL Server How

    4/5

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Name

    Comment

    SQL Server, T-SQL

    Product Support Services Developer - SQL Server Core Engineer |

    Leave a Comment

    Post

    Comments

    Torsten 7 Feb 2013 7:47 AM

    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]

    on't change anything.

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fblogs.technet.com%2fb%2fmdegre%2farchive%2f2009%2f09%2f04%2fsql-server-how-to-insert-data-from-one-table-to-another-table.aspx&id=ma-130920032234-d760c03dhttp://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://www.facebook.com/sharer.php?u=http://blogs.technet.com/b/mdegre/archive/2009/09/04/sql-server-how-to-insert-data-from-one-table-to-another-table.aspx&t=How+to+insert+data+from+one+table+to+another+table+http://twitter.com/home?status=How+to+insert+data+from+one+table+to+another+table+%20:%20http://blogs.technet.com/b/mdegre/archive/2009/09/04/sql-server-how-to-insert-data-from-one-table-to-another-table.aspxhttp://digg.com/submit?url=http://blogs.technet.com/b/mdegre/archive/2009/09/04/sql-server-how-to-insert-data-from-one-table-to-another-table.aspx&title=How+to+insert+data+from+one+table+to+another+table+http://del.icio.us/post?url=http://blogs.technet.com/b/mdegre/archive/2009/09/04/sql-server-how-to-insert-data-from-one-table-to-another-table.aspx&title=How+to+insert+data+from+one+table+to+another+table+http://blogs.technet.com/b/mdegre/archive/tags/SQL+Server/http://blogs.technet.com/b/mdegre/archive/tags/T_2D00_SQL/http://webform_dopostbackwithoptions%28new%20webform_postbackoptions%28%22ctl00%24content%24ctl00%24w_12499%24_b7e93d%24ctl00%24ctl00%24ctl00%24ctl05%24bpcommentform%24ctl05%24btnsubmit%22%2C%20%22%22%2C%20true%2C%20%22blogpostcommentform-ctl00_content_ctl00_w_12499__b7e93d_ctl00_ctl00%22%2C%20%22%22%2C%20false%2C%20true%29%29/http://blogs.technet.com/b/mdegre/rsscomments.aspx?WeblogPostID=3278803
  • 7/27/2019 Blogs Technet Com b Mdegre Archive 2009-09-04 SQL Server How

    5/5