{"id":6815,"date":"2016-05-29T14:46:58","date_gmt":"2016-05-29T06:46:58","guid":{"rendered":"http:\/\/onthe8spot.com\/?p=6815"},"modified":"2016-05-29T14:46:58","modified_gmt":"2016-05-29T06:46:58","slug":"oracle-sql-update-a-table-with-data-from-another-table","status":"publish","type":"post","link":"http:\/\/onthe8spot.com\/index.php\/2016\/05\/29\/oracle-sql-update-a-table-with-data-from-another-table\/","title":{"rendered":"Oracle SQL: Update a table with data from another table"},"content":{"rendered":"<p>First time I used a correlated update in a script I had to do.<\/p>\n<blockquote><p>In oracle SQL, how do I run an <strong>sql update<\/strong> query that can update Table 1 with Table 2&#8217;s name and desc using the same id?<br \/>\nThis is called a correlated update<\/p>\n<pre class=\"lang-sql prettyprint prettyprinted\"><code><span class=\"kwd\">UPDATE<\/span><span class=\"pln\"> table1 t1\n   <\/span><span class=\"kwd\">SET<\/span> <span class=\"pun\">(<\/span><span class=\"pln\">name<\/span><span class=\"pun\">,<\/span> <span class=\"kwd\">desc<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">=<\/span> <span class=\"pun\">(<\/span><span class=\"kwd\">SELECT<\/span><span class=\"pln\"> t2<\/span><span class=\"pun\">.<\/span><span class=\"pln\">name<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> t2<\/span><span class=\"pun\">.<\/span><span class=\"kwd\">desc<\/span>\n                         <span class=\"kwd\">FROM<\/span><span class=\"pln\"> table2 t2\n                        <\/span><span class=\"kwd\">WHERE<\/span><span class=\"pln\"> t1<\/span><span class=\"pun\">.<\/span><span class=\"pln\">id <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> t2<\/span><span class=\"pun\">.<\/span><span class=\"pln\">id<\/span><span class=\"pun\">)<\/span>\n <span class=\"kwd\">WHERE<\/span> <span class=\"kwd\">EXISTS<\/span> <span class=\"pun\">(<\/span>\n    <span class=\"kwd\">SELECT<\/span> <span class=\"lit\">1<\/span>\n      <span class=\"kwd\">FROM<\/span><span class=\"pln\"> table2 t2\n     <\/span><span class=\"kwd\">WHERE<\/span><span class=\"pln\"> t1<\/span><span class=\"pun\">.<\/span><span class=\"pln\">id <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> t2<\/span><span class=\"pun\">.<\/span><span class=\"pln\">id <\/span><span class=\"pun\">)<\/span><\/code><\/pre>\n<p>Assuming the join results in a key-preserved view, you could also<\/p>\n<pre class=\"lang-sql prettyprint prettyprinted\"><code><span class=\"kwd\">UPDATE<\/span> <span class=\"pun\">(<\/span><span class=\"kwd\">SELECT<\/span><span class=\"pln\"> t1<\/span><span class=\"pun\">.<\/span><span class=\"pln\">id<\/span><span class=\"pun\">,<\/span><span class=\"pln\">\n               t1<\/span><span class=\"pun\">.<\/span><span class=\"pln\">name name1<\/span><span class=\"pun\">,<\/span><span class=\"pln\">\n               t1<\/span><span class=\"pun\">.<\/span><span class=\"kwd\">desc<\/span><span class=\"pln\"> desc1<\/span><span class=\"pun\">,<\/span><span class=\"pln\">\n               t2<\/span><span class=\"pun\">.<\/span><span class=\"pln\">name name2<\/span><span class=\"pun\">,<\/span><span class=\"pln\">\n               t2<\/span><span class=\"pun\">.<\/span><span class=\"kwd\">desc<\/span><span class=\"pln\"> desc2\n          <\/span><span class=\"kwd\">FROM<\/span><span class=\"pln\"> table1 t1<\/span><span class=\"pun\">,<\/span><span class=\"pln\">\n               table2 t2\n         <\/span><span class=\"kwd\">WHERE<\/span><span class=\"pln\"> t1<\/span><span class=\"pun\">.<\/span><span class=\"pln\">id <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> t2<\/span><span class=\"pun\">.<\/span><span class=\"pln\">id<\/span><span class=\"pun\">)<\/span>\n   <span class=\"kwd\">SET<\/span><span class=\"pln\"> name1 <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> name2<\/span><span class=\"pun\">,<\/span><span class=\"pln\">\n       desc1 <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> desc2<\/span><\/code><\/pre>\n<\/blockquote>\n<p>&nbsp;<br \/>\n&nbsp;<br \/>\nSource: <em><a href=\"http:\/\/stackoverflow.com\/questions\/7030699\/oracle-sql-update-a-table-with-data-from-another-table\">Oracle SQL: Update a table with data from another table<\/a><\/em><br \/>\n&nbsp;<br \/>\nGuide to the select form found <a href=\"http:\/\/psoug.org\/snippet\/UPDATE-Update-from-a-SELECT-statement_601.htm\">here<\/a>.<br \/>\n&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First time I used a correlated update in a script I had to do. In oracle SQL, how do I run an sql update query that can update Table 1 with Table 2&#8217;s name and desc using the same id? This is called a correlated update UPDATE table1 t1 SET (name, desc) = (SELECT t2.name, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/onthe8spot.com\/index.php\/2016\/05\/29\/oracle-sql-update-a-table-with-data-from-another-table\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Oracle SQL: Update a table with data from another table&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[18],"tags":[],"class_list":["post-6815","post","type-post","status-publish","format-standard","hentry","category-coding"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/posts\/6815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/comments?post=6815"}],"version-history":[{"count":0,"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/posts\/6815\/revisions"}],"wp:attachment":[{"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/media?parent=6815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/categories?post=6815"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/tags?post=6815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}