<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Zoboki Árpád</title>
    <description>The latest articles on Forem by Zoboki Árpád (@zopad).</description>
    <link>https://forem.com/zopad</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F323388%2F320cabe2-5b8f-4700-97cd-69e99f22899d.png</url>
      <title>Forem: Zoboki Árpád</title>
      <link>https://forem.com/zopad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/zopad"/>
    <language>en</language>
    <item>
      <title>Loop on static content to upsert many rows in Oracle SQL</title>
      <dc:creator>Zoboki Árpád</dc:creator>
      <pubDate>Thu, 13 Apr 2023 08:01:43 +0000</pubDate>
      <link>https://forem.com/zopad/loop-on-static-content-to-upsert-many-rows-in-oracle-sql-39gl</link>
      <guid>https://forem.com/zopad/loop-on-static-content-to-upsert-many-rows-in-oracle-sql-39gl</guid>
      <description>&lt;p&gt;Sometimes you just want to add many test rows into your Oracle DB, by iterating over fixed content. If your DB already has some rows which you then need to update, then what you're looking for is the upsert (update or insert) operation.&lt;/p&gt;

&lt;p&gt;Here's one example how to do this in Oracle SQL, without creating a temporary table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;begin
for i in (select * from table(sys.dbms_debug_vc2coll('1234','5678')))
loop
merge into my_target_table target
using (
  select i.COLUMN_VALUE as acc_no,
  'N' as static_val,
  '1' as another_static_field
  from dual
  ) temp on (target.acc_no = temp.acc_no)
when matched then
  update set target.MY_FIELD_TO_UPDATE = temp.static_val, target.SECOND_FIELD = temp.another_static_field
when not matched then
  insert values (temp.acc_no, temp.static_val, temp.another_static_field);
end loop;
end;
/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All parts of this syntax are necessary, including the ending / which signifies the end of the anonymous PL/SQL code block, so you can execute it in e.g. SQuirrel SQL.&lt;br&gt;
&lt;code&gt;sys.dbms_debug_vc2coll('1234','5678')&lt;/code&gt; this call outputs whatever list of data you need to loop over&lt;br&gt;
&lt;code&gt;from dual&lt;/code&gt; using the special dual table keyword, we don't have to create a table to hold our static data, it will be in memory only&lt;br&gt;
&lt;code&gt;when matched then&lt;/code&gt; the matched directive allows us to do the upsert operation (update when already existing, insert when new)&lt;br&gt;
&lt;code&gt;i.COLUMN_VALUE&lt;/code&gt; pay special attention to the .COLUMN_VALUE field, which is how Oracle allows us to access each single value that is being looped over&lt;/p&gt;

</description>
      <category>oracle</category>
      <category>sql</category>
      <category>programming</category>
      <category>database</category>
    </item>
  </channel>
</rss>
