{"id":29219,"date":"2023-11-14T06:42:00","date_gmt":"2023-11-14T05:42:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=29219"},"modified":"2023-11-15T21:58:05","modified_gmt":"2023-11-15T20:58:05","slug":"update-table-from-select-in-sql-server","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/","title":{"rendered":"Update a table from a Select in SQL Server"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\" id=\"h-how-to-update-rows-in-a-table-directly-from-a-select-statement-in-sql-server\"><em>How to update rows in a table directly from a Select statement in SQL Server?<\/em><\/h4>\n\n\n\n<p>Technical tutorial to update a table directly from an select in SQL Server. Indeed, generally, in data integration projects, it often involves to update table based on the data from another table. This is particularly common in scenarios where you have separate but related datasets, such as a source table with new sales data and a target table where this data needs to be integrated.<\/p>\n\n\n\n<p>Here, we will explore how to update a table named SalesDataTarget from a table named SalesDataSource using three methods: using <a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-select-queries-to-filter-data\/\"><strong>SELECT<\/strong><\/a> with INNER JOIN, the MERGE INTO option and using a temporary table. Before diving into the queries, let&#8217;s define below the structure of our example tables.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#1-prepare-sql-server-tables-for-the-update\" >1. Prepare SQL Server tables for the update<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#2-use-select-and-inner-join-to-update-the-table\" >2. Use Select and Inner Join to Update the table<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#3-use-the-merge-into-for-update-or-insert\" >3. Use the Merge Into for update or insert<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#4-t-sql-update-script-with-a-temporary-table-for-performance\" >4. T-SQL update script with a temporary table for performance<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-prepare-sql-server-tables-for-the-update\"><span class=\"ez-toc-section\" id=\"1-prepare-sql-server-tables-for-the-update\"><\/span>1. Prepare SQL Server tables for the update<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In the source table and also in the updated target table, we use these 5 columns: <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Month<\/li>\n\n\n\n<li>Year<\/li>\n\n\n\n<li>Product<\/li>\n\n\n\n<li>Amount<\/li>\n\n\n\n<li>Quantity<\/li>\n<\/ol>\n\n\n\n<p>And an additional metadata column in the table to update, to store the <em>modified date<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-create-the-source-table-and-populate-it\">Create the source table and populate it<\/h3>\n\n\n\n<p>Use this script to create the first table.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE SalesDataSource (\n    Month INT,\n    Year INT,\n    Product VARCHAR(100),\n    Amount DECIMAL(10, 2),\n    Quantity INT\n);\n<\/pre>\n\n\n\n<p>Then insert a few sample rows to illustrate how the update query works.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">INSERT INTO SalesDataSource \n(Month, Year, Product, Amount, Quantity)\nVALUES \n(1, 2023, 'ProductA', 100.00, 10),\n(1, 2023, 'ProductB', 200.00, 20),\n(2, 2023, 'ProductA', 150.00, 15),\n(2, 2023, 'ProductB', 250.00, 25),\n(3, 2023, 'ProductA', 120.00, 12),\n(3, 2023, 'ProductB', 220.00, 22),\n(4, 2023, 'ProductA', 130.00, 13),\n(4, 2023, 'ProductB', 230.00, 23);\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-create-and-populate-the-table-to-update\">Create and populate the table to update<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE SalesDataTarget (\n    Month INT,\n    Year INT,\n    Product VARCHAR(100),\n    Amount DECIMAL(10, 2),\n    Quantity INT,\n    ModifiedDate DATETIME\n);\n\nINSERT INTO SalesDataTarget \n(Month, Year, Product, Amount, Quantity, ModifiedDate)\nVALUES \n(1, 2023, 'ProductA', 90.00,   9, GETDATE()),\n(1, 2023, 'ProductB', 190.00, 19, GETDATE()),\n(2, 2023, 'ProductA', 140.00, 14, GETDATE()),\n(2, 2023, 'ProductB', 240.00, 24, GETDATE()),\n(3, 2023, 'ProductA', 110.00, 11, GETDATE()),\n(3, 2023, 'ProductB', 210.00, 21, GETDATE()),\n(4, 2023, 'ProductA', 130.00, 13, GETDATE()),\n(4, 2023, 'ProductB', 230.00, 23, GETDATE());\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-2-use-select-and-inner-join-to-update-the-table\"><span class=\"ez-toc-section\" id=\"2-use-select-and-inner-join-to-update-the-table\"><\/span>2. Use Select and Inner Join to Update the table<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The SELECT statement combined with <a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-join-operator\/\"><strong>INNER JOIN<\/strong><\/a> is a straightforward approach for updating records. This method is particularly useful when you need to update existing records in the target table based on matching criteria.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">UPDATE T\nSET T.Amount       = S.Amount,\n    T.Quantity     = S.Quantity,\n    T.ModifiedDate = GETDATE()\nFROM       SalesDataTarget AS T\nINNER JOIN SalesDataSource AS S\nON     T.Month   = S.Month\n   AND T.Year    = S.Year\n   AND T.Product = S.Product\nWHERE \n\/* Add here conditions to filter rows   *\/\n\/* do not forget the alias of the table *\/\n;\n<\/pre>\n\n\n\n<p>In the query above, the <em>SalesDataTarget<\/em> table is updated with the <em>Amount<\/em> and <em>Quantity<\/em> from <em>SalesDataSource<\/em>, where the Month, Year, and Product columns match. The ModifiedDate is set to the current date and time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-3-use-the-merge-into-for-update-or-insert\"><span class=\"ez-toc-section\" id=\"3-use-the-merge-into-for-update-or-insert\"><\/span>3. Use the Merge Into for update or insert<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>The MERGE INTO statement allows updating, inserting, and deleting.<\/strong> So it is more versatile and can be used for complex operations like simultaneous .<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MERGE INTO \n      SalesDataTarget AS T\nUSING SalesDataSource AS S\nON     T.Month   = S.Month\n   AND T.Year    = S.Year\n   AND T.Product = S.Product\nWHEN MATCHED THEN\n    UPDATE SET T.Amount       = S.Amount,\n               T.Quantity     = S.Quantity,\n               T.ModifiedDate = GETDATE()\nWHEN NOT MATCHED BY TARGET THEN\n   INSERT\n      (Month, Year, Product, Amount, Quantity, ModifiedDate)\n   VALUES\n      (S.Month, S.Year, S.Product, S.Amount, S.Quantity, GETDATE())\n;<\/pre>\n\n\n\n<p>In this MERGE INTO statement, the <em>SalesDataTarget<\/em> table is <a href=\"https:\/\/dictionary.cambridge.org\/dictionary\/english\/update\">updated<\/a> with values from <em>SalesDataSource<\/em> based on matching <code>Month<\/code>, <code>Year<\/code>, and <code>Product<\/code>. If a matching row doesn\u2019t exist in the target, a new row is inserted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-4-t-sql-update-script-with-a-temporary-table-for-performance\"><span class=\"ez-toc-section\" id=\"4-t-sql-update-script-with-a-temporary-table-for-performance\"><\/span>4. T-SQL update script with a temporary table for performance<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In the script below, we perform these actions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A temporary table named #TempSalesData is created, mirroring the structure of SalesDataSource.<\/li>\n\n\n\n<li>Data is inserted into #TempSalesData from SalesDataSource.<\/li>\n\n\n\n<li>SalesDataTarget is updated using the data from #TempSalesData, with matching on Month, Year, and Product. The ModifiedDate in SalesDataTarget is updated to the current date and time.<\/li>\n\n\n\n<li>Finally, the temporary table is dropped with DROP TABLE.<\/li>\n<\/ul>\n\n\n\n<p>This approach is useful when you need to perform intermediate operations on the data before updating the target table, or when you want to reduce the load on the original source table during complex operations. Especially if you load a very large amount of data and you only want to use the selected data in the join of the update operation.<\/p>\n\n\n\n<p><a href=\"https:\/\/expert-only.com\/en\/t-sql\/manage-sql-server-temporary-tables\/\"><strong>Temporary tables are session-specific in SQL Server<\/strong><\/a> and are automatically dropped when the session ends, but it&#8217;s good practice to explicitly drop them as shown.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Create a temporary table with the same structure as SalesDataSource\nCREATE TABLE #TempSalesData (\n    Month INT,\n    Year INT,\n    Product VARCHAR(100),\n    Amount DECIMAL(10, 2),\n    Quantity INT\n);\n\n-- Insert data into the temporary table from SalesDataSource\nINSERT INTO #TempSalesData\n   (Month, Year, Product, Amount, Quantity)\nSELECT Month, Year, Product, Amount, Quantity\nFROM   SalesDataSource\nWHERE  Product = 'ProductA' -- filter example\n   AND Month = 1;           -- filter example\n\n-- Update SalesDataTarget using the temporary table\nUPDATE Target\nSET Target.Amount = Temp.Amount,\n    Target.Quantity = Temp.Quantity,\n    Target.ModifiedDate = GETDATE()\nFROM SalesDataTarget AS Target\nINNER JOIN #TempSalesData AS Temp\nON Target.Month = Temp.Month\n   AND Target.Year = Temp.Year\n   AND Target.Product = Temp.Product;\n\n-- Drop the temporary table when done\nDROP TABLE #TempSalesData;\n<\/pre>\n\n\n\n<p> <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-different-approaches-to-update-tables-with-slightly-different-outputs\">Different approaches to update tables with slightly different outputs <\/h3>\n\n\n\n<p>Both these methods are effective for synchronizing data between two tables in SQL Server. The choice of method depends on your specific requirements and the complexity of the data operation. The <code>SELECT<\/code> with <code>INNER JOIN<\/code> is ideal for straightforward updates, while <code>MERGE INTO<\/code> provides a more comprehensive solution for handling multiple types of data operations in a single statement.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-more-tutorials-on-the-sql-server-update-topic\">More tutorials on the SQL Server UPDATE topic<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/expert-only.com\/en\/t-sql\/insert-or-update-with-sql-server\/\"><strong>Insert or Update with SQL Server: Upsert<\/strong><\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/t-sql\/update-same-column-another-line-sql\/\"><strong>Update the same column of another line with SQL Server<\/strong><\/a><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"7gHhFVyd1Q\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/last-time-table-was-updated-sql-server\/\">Last time table was updated in SQL Server<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Last time table was updated in SQL Server&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/last-time-table-was-updated-sql-server\/embed\/#?secret=82wt7DkeR2#?secret=7gHhFVyd1Q\" data-secret=\"7gHhFVyd1Q\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>How to update rows in a table directly from a Select statement in SQL Server? Technical tutorial to update a table directly from an select in SQL Server. Indeed, generally, in data integration projects, it often involves to update <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/\" title=\"Update a table from a Select in SQL Server\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10654,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-29219","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Update a table from a Select in SQL Server - T-SQL<\/title>\n<meta name=\"description\" content=\"Use these SQL Server scripts to update a target table directly from a select statement using an inner join, a merge, or a temporary table.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Update a table from a Select in SQL Server\" \/>\n<meta property=\"og:description\" content=\"Use these SQL Server scripts to update a target table directly from a select statement using an inner join, a merge, or a temporary table.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-14T05:42:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-15T20:58:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"Update a table from a Select in SQL Server\",\"datePublished\":\"2023-11-14T05:42:00+00:00\",\"dateModified\":\"2023-11-15T20:58:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/\"},\"wordCount\":608,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpg\",\"articleSection\":[\"T-SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/\",\"url\":\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/\",\"name\":\"Update a table from a Select in SQL Server - T-SQL\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpg\",\"datePublished\":\"2023-11-14T05:42:00+00:00\",\"dateModified\":\"2023-11-15T20:58:05+00:00\",\"description\":\"Use these SQL Server scripts to update a target table directly from a select statement using an inner join, a merge, or a temporary table.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Update a table from a Select in SQL Server\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Update a table from a Select in SQL Server - T-SQL","description":"Use these SQL Server scripts to update a target table directly from a select statement using an inner join, a merge, or a temporary table.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/","og_locale":"en_US","og_type":"article","og_title":"Update a table from a Select in SQL Server","og_description":"Use these SQL Server scripts to update a target table directly from a select statement using an inner join, a merge, or a temporary table.","og_url":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2023-11-14T05:42:00+00:00","article_modified_time":"2023-11-15T20:58:05+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"Update a table from a Select in SQL Server","datePublished":"2023-11-14T05:42:00+00:00","dateModified":"2023-11-15T20:58:05+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/"},"wordCount":608,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpg","articleSection":["T-SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/","url":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/","name":"Update a table from a Select in SQL Server - T-SQL","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpg","datePublished":"2023-11-14T05:42:00+00:00","dateModified":"2023-11-15T20:58:05+00:00","description":"Use these SQL Server scripts to update a target table directly from a select statement using an inner join, a merge, or a temporary table.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/t-sql\/update-table-from-select-in-sql-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"Update a table from a Select in SQL Server"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/29219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=29219"}],"version-history":[{"count":17,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/29219\/revisions"}],"predecessor-version":[{"id":29240,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/29219\/revisions\/29240"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10654"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=29219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=29219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=29219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}