Insights · Migration

Nine things a plain T-SQL translation gets wrong on Databricks

Translating stored procedures statement by statement is the fastest way to start a Databricks migration. It is also how teams end up with code that compiles, runs, and quietly returns different numbers.

While building sproc-to-lakehouse, our open-source migration tool, we ran twelve synthetic SQL Server stored procedures through sqlglot, a widely used open-source SQL translator, and executed the output on Apache Spark with Delta Lake. Then we ran the originals on SQL Server 2022 against the same seeded data and compared the results row by row.

With the plain translation, only two of six fully convertible procedures returned the same data as SQL Server. The other four failed on Spark. Every failure traced back to one of the cases below.

The point is not that translators are bad. sqlglot handled most statements well. The point is that a translation is a first draft, and only a parity test tells you which drafts are safe.

The nine cases

T-SQLPlain translation on SparkWhat to do instead
'ID-' + CAST(n AS VARCHAR)Treated as numeric addition: DATATYPE_MISMATCHRewrite string concatenation as CONCAT(...)
TRY_CONVERT(DATE, s, 101)Becomes TO_DATE, which fails on bad inputCAST(TRY_TO_TIMESTAMP(s, 'M/d/yyyy') AS DATE)
DATEPART(ISO_WEEK, d)EXTRACT(WEEKISO ...): INVALID_EXTRACT_FIELDEXTRACT(WEEK FROM d)
UPDATE t ... FROM t JOIN sDatabricks UPDATE has no FROM: parse errorMERGE INTO, de-duplicating the source first
TRUNCATE TABLE tRejected by open-source Delta LakeDELETE FROM t
flag_bit = 1BIT arrives as BOOLEAN: type mismatchCompare with TRUE, using the table DDL
ISNULL(d, '19000101')The string cannot be cast to DATECAST('1900-01-01' AS DATE), using the DDL
STUFF(s, 4, 3, '***')Passed through unchanged: unknown functionFlag for a person to rewrite
DATEDIFF(DAY, 23:00, 01:00 next day)Returns 0 on Spark; SQL Server returns 1Convert with a review note

Three patterns behind the failures

1. Same syntax, different meaning

The + operator joins strings in T-SQL and adds numbers in Spark SQL. DATEDIFF(DAY, ...) counts the day boundaries crossed in SQL Server, while the translated Spark expression counts whole days elapsed. These translate cleanly and fail, or worse, succeed with a different answer. The DATEDIFF case never errors; you only find it by comparing outputs.

2. The translator can't see your schema

A statement-level translator doesn't know that flag_bit was a BIT column that is now a BOOLEAN, or that '19000101' is being compared with a DATE. Fixing these needs the table definitions. Our tool reads the DDL alongside the procedures for exactly this reason.

3. Some constructs have no safe one-line equivalent

UPDATE ... FROM with a join has to become a MERGE, and that is only correct if the source has one row per key. Cursors, dynamic SQL, transactions and TRY/CATCH blocks need a person to decide how the logic should look on Databricks. A tool that guesses here creates risk you can't see. Ours flags them as manual steps instead.

What we do instead

  1. Classify before converting. Every construct is marked automatic, automatic with a note, or needs a person.
  2. Fix what a translation gets wrong, using the table DDL, before generating dbt models and notebooks.
  3. Validate on Spark. Every generated statement must parse and resolve.
  4. Prove parity. Run old and new code on the same data, and compare row counts, column checksums and row-level differences by key, on the rows the procedure actually changed.

With those fixes, all six fully convertible procedures in our test suite matched SQL Server. The results, code and reports are public: see the sproc-to-lakehouse results.

Planning a stored procedure migration? Our legacy migration to Databricks service applies this method to your code, with a parity report for every procedure. Or book a free 30-minute call.