Skip to content
ITACC
Abstract flowing lines of light

Data Migration

Oracle cursor loops on Databricks: what changes when row-by-row code moves

By Ed, CTO @ ITACC · · 4 min read

Key takeaways

  • Oracle cursor loops handle one row at a time; Databricks is built to handle whole sets of rows at once.
  • Copying the loop as it is can make jobs slow. Changing its shape can change the results.
  • The risky part is hidden: loop order, per-row commits and partial failures carry business meaning.
  • Converted code can pass review and test runs and still disagree with Oracle on real data.
  • Across hundreds of procedures, proving every loop still gives the same answer is specialist work.

Short answer: most Oracle PL/SQL estates are full of cursor loops: code that reads rows one by one and decides what to do with each. Databricks can run loops too, but it's designed to work on whole sets of rows in parallel. Keep the loop exactly as it was and jobs can crawl. Change its shape to suit Databricks and the results can quietly change, because the loop often carried rules nobody wrote down: which row wins, what happens when one row fails, and what was already saved when it did.

This is the third article in our series on Oracle to Databricks migration, after Oracle empty strings and NULL and Oracle NUMBER and Databricks DECIMAL. For the bigger picture, see our guide to migrating Oracle stored procedures to Databricks.

Two ways of working through data

In Oracle, the cursor FOR LOOP runs a query and then loops through its result set. With each iteration it fetches a row into a record, and the code inside the loop can check values, update tables, count, commit or raise an error before the next row arrives. Developers have relied on this style for decades, and it's often where the real business rules live.

Databricks SQL scripting has a FOR statement as well, available in Databricks SQL and Databricks Runtime 16.3 and above, which repeats a list of statements for each row a query returns. But the platform's strength is processing large sets of rows at once, and Databricks' own documentation notes that FOR loops can often be replaced with relational queries, which are typically more efficient. That leaves every migration with a choice for every loop, and neither option is free.

Where it shows up

When cursor loops move from Oracle to Databricks, the differences typically surface as:

  • Batch windows that blow up. A nightly job that finished comfortably on Oracle can run far longer once it issues one statement per row.
  • A different row wins. When several rows update the same record, the loop let the last one win. Change the order or the shape, and a different value is kept.
  • Jobs that fail on duplicates. Logic that quietly overwrote a record twice on Oracle now stops with an error on the first day two rows share a key.
  • Partial loads that no longer match. On Oracle, a loop that committed as it went left the earlier rows saved when a later row failed. Downstream processes and support teams expect exactly that state.
  • Counters and flags that drift. Row counts, "first row" checks and running totals kept inside the loop come out differently.

Most of these raise no error. The job finishes, the tables fill up, and the differences wait for month-end.

A real example

One thing our validation runs caught was a loop that walked through transactions and updated a summary record for each one. On Oracle, when two transactions touched the same record, the second simply overwrote the first. In the converted version, the same data either kept a different value or stopped with an error, depending on how the batch was shaped. Every test row with one transaction per record matched perfectly. The difference appeared only on real-looking data, where duplicates are normal.

Another run showed the failure side: an Oracle loop raised an error halfway through a batch, after saving the rows before it. The converted job processed the batch as a whole, so after the same error the table didn't hold the rows Oracle had already written. Anything downstream that expected Oracle's partial result would find something else.

Neither stood out in code review. The converted code looked clean and ran. Only comparing the new results with the legacy results, on the same inputs, showed what had changed.

Why it's hard to catch at scale

One loop is easy to reason about. A real Oracle estate has hundreds, written by different people over many years:

  • It spreads. A loop that keeps the wrong row feeds balances, allocations and reports several steps later.
  • It hides. It depends on data shape, so clean test data and sample checks rarely trigger it.
  • It's not alone. Loops combine with empty-string handling, numeric precision, error handling and commits, and each makes the others harder to see.
  • It needs decisions. Which row should win, what a failed batch should leave behind, and how long a job may run are business calls, not coding details.

That's why loop-heavy procedures are where migration timelines slip. Rewriting them is the easy part. Proving they still behave the same is not.

How ITACC handles it

ITACC is a Databricks Brickbuilder Bronze Partner, part of Databricks' partner program, and we work with companies across North America, in Canada and the US, moving Oracle workloads onto Databricks.

We've built our own migration tooling and method around one principle: every migrated object is proven against your legacy results, or flagged with the reason. Nothing silently wrong.

We convert Oracle SQL and PL/SQL, cursor loops included, to Databricks while keeping the behaviour your business logic depends on, and we hand you the evidence: what matches, what differs, and why. Our migration tooling runs inside your own environment, so your data stays where you keep it, including in Canada. Your team spends its time on decisions, not on chasing rows.

The next article in this series covers exceptions and error behaviour after migration. If you also run Informatica, see what changes moving PowerCenter to Databricks.

ITACC is an AI and data engineering partner based in Oakville, Ontario, and a Databricks Brickbuilder Bronze Partner. Learn more about our data and workflow automation work.

Frequently asked questions

Can Databricks run Oracle-style cursor loops?

Yes. Databricks SQL scripting has a FOR statement that repeats statements for each row a query returns, in Databricks SQL and Databricks Runtime 16.3 and above. Whether a loop should stay a loop is a separate question, because Databricks is built for set-based processing.

Will our row-by-row jobs be slower on Databricks?

They can be. A loop that sends one statement per row works against a platform designed to process many rows in parallel, and run time tends to grow with data volume. How much it matters depends on each job and its batch window.

If the converted code runs without errors, are the results the same?

Not necessarily. Loops often carry hidden rules, such as which row wins, what a failure leaves behind and how counters are kept. Converted code can run cleanly and still produce different results on real data.

How do we know which of our procedures are affected?

Most Oracle estates use cursor loops heavily, so the question is where the differences matter. The reliable answer comes from comparing migrated outputs with legacy outputs on real data. We can show you on one of your own procedures.

Abstract flowing lines of light

Ready to take AI past the demo?

Tell us what you want to build. We'll reply within one business day, and we're happy to sign an NDA first.