<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic oracle sequence number in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/oracle-sequence-number/m-p/137059#M50702</link>
    <description>&lt;P&gt;Dear All,&lt;/P&gt;&lt;P&gt;I am trying to use jdbc driver to connect to an oracle database and append a new record to a table. The table has a column needs to be populated with a sequence number. I've been trying to use select `&amp;lt;sequence_name&amp;gt;.nextval` to get the sequence number but got the `&lt;SPAN&gt;ORA-02287: sequence number not allowed here` error. I did some research online and some said we can't do this with pyspark so I wonder if anyone was able to do this without writing pure python code? Thanks so much!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 31 Oct 2025 15:54:58 GMT</pubDate>
    <dc:creator>austinoyoung</dc:creator>
    <dc:date>2025-10-31T15:54:58Z</dc:date>
    <item>
      <title>oracle sequence number</title>
      <link>https://community.databricks.com/t5/data-engineering/oracle-sequence-number/m-p/137059#M50702</link>
      <description>&lt;P&gt;Dear All,&lt;/P&gt;&lt;P&gt;I am trying to use jdbc driver to connect to an oracle database and append a new record to a table. The table has a column needs to be populated with a sequence number. I've been trying to use select `&amp;lt;sequence_name&amp;gt;.nextval` to get the sequence number but got the `&lt;SPAN&gt;ORA-02287: sequence number not allowed here` error. I did some research online and some said we can't do this with pyspark so I wonder if anyone was able to do this without writing pure python code? Thanks so much!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Oct 2025 15:54:58 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/oracle-sequence-number/m-p/137059#M50702</guid>
      <dc:creator>austinoyoung</dc:creator>
      <dc:date>2025-10-31T15:54:58Z</dc:date>
    </item>
    <item>
      <title>Re: oracle sequence number</title>
      <link>https://community.databricks.com/t5/data-engineering/oracle-sequence-number/m-p/137102#M50705</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/170483"&gt;@austinoyoung&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;Short answer: Don’t try to pull the sequence in your Spark insert. Let Oracle assign it.&lt;/P&gt;
&lt;P class="p4"&gt;&lt;SPAN class="s2"&gt;Why this happens (&lt;/SPAN&gt;ORA-02287: sequence number not allowed here&lt;SPAN class="s2"&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="p1"&gt;Spark’s JDBC writer generates parameterized INSERT statements like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;INSERT INTO your_table (id, col_a, col_b) VALUES (?, ?, ?)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P class="p1"&gt;The &lt;SPAN class="s1"&gt;?&lt;/SPAN&gt; parameters must be literal values. Oracle sequences (e.g., &lt;SPAN class="s1"&gt;myseq.nextval&lt;/SPAN&gt;) are expressions and cannot be used in that context. That’s why Oracle rejects the insert.&lt;/P&gt;
&lt;P class="p1"&gt;Recommended approaches (no pure Python loops)&lt;/P&gt;
&lt;OL start="1"&gt;
&lt;LI&gt;
&lt;P class="p1"&gt;Identity column (Oracle 12c+)&lt;/P&gt;
&lt;P class="p1"&gt;If you can alter the table, make the key auto-generated by Oracle:&lt;/P&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE&gt;ALTER TABLE your_table
  MODIFY (id NUMBER GENERATED BY DEFAULT AS IDENTITY);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P class="p1"&gt;Then drop the id column before writing from Spark:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;df.drop("id").write \
    .format("jdbc") \
    .option("url", jdbcUrl) \
    .option("dbtable", "your_table") \
    .option("user", user) \
    .option("password", pwd) \
    .mode("append") \
    .save()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL start="2"&gt;
&lt;LI&gt;
&lt;P class="p1"&gt;Default value backed by a sequence (also Oracle 12c+)&lt;/P&gt;
&lt;P class="p1"&gt;Keep your sequence but make the column default to it:&lt;/P&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE&gt;ALTER TABLE your_table
  MODIFY (id NUMBER DEFAULT your_seq.NEXTVAL);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P class="p1"&gt;Then, again, don’t send id from Spark.&lt;/P&gt;
&lt;OL start="3"&gt;
&lt;LI&gt;
&lt;P class="p1"&gt;BEFORE INSERT trigger (works on older Oracle versions)&lt;/P&gt;
&lt;P class="p1"&gt;If identity/default isn’t allowed:&lt;/P&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE&gt;CREATE OR REPLACE TRIGGER your_table_bi
BEFORE INSERT ON your_table
FOR EACH ROW
WHEN (NEW.id IS NULL)
BEGIN
  :NEW.id := your_seq.NEXTVAL;
END;
/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P class="p1"&gt;Omit id in your DataFrame and Oracle will set it automatically.&lt;/P&gt;
&lt;P class="p1"&gt;Why not generate IDs in Spark?&lt;/P&gt;
&lt;P class="p1"&gt;Functions like &lt;SPAN class="s1"&gt;monotonically_increasing_id()&lt;/SPAN&gt; or UUIDs won’t increment the Oracle sequence, and can cause gaps or ordering issues if other systems rely on Oracle’s sequence.&lt;/P&gt;
&lt;P class="p1"&gt;Summary&lt;/P&gt;
&lt;UL&gt;
&lt;LI class="p1"&gt;Don’t try to select the sequence in your Spark insert.&lt;/LI&gt;
&lt;LI class="p1"&gt;Let Oracle handle the ID via identity, default+sequence, or a trigger.&lt;/LI&gt;
&lt;LI class="p1"&gt;Just drop the id column from your DataFrame before writing.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;Hope this helps, Louis.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Oct 2025 17:31:17 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/oracle-sequence-number/m-p/137102#M50705</guid>
      <dc:creator>Louis_Frolio</dc:creator>
      <dc:date>2025-10-31T17:31:17Z</dc:date>
    </item>
    <item>
      <title>Re: oracle sequence number</title>
      <link>https://community.databricks.com/t5/data-engineering/oracle-sequence-number/m-p/137104#M50706</link>
      <description>&lt;P&gt;Thank you, Louis, for the information!&lt;/P&gt;&lt;P&gt;I really like the identity column solution, but I have to reach out to our dba for implementation.&lt;span class="lia-unicode-emoji" title=":crossed_fingers:"&gt;🤞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Fri, 31 Oct 2025 17:56:03 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/oracle-sequence-number/m-p/137104#M50706</guid>
      <dc:creator>austinoyoung</dc:creator>
      <dc:date>2025-10-31T17:56:03Z</dc:date>
    </item>
  </channel>
</rss>

