Rewriting an Oracle procedure with cursor in a T-SQL procedure

pmscorca 817 Reputation points
2024-03-23T20:46:10.0933333+00:00

Hi,

I need to rewrite an Oracle procedure that uses heavily a cursor in a T-SQL stored procedure. An Oracle cursor is an object more efficient/performant than a T-SQL cursor and moreover the Oracle procedure logic works row by row and has several IF ... ELSE more hard to convert in T-SQL set-based operations.

Any ideas to solve a such issue? Thanks

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,945 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,566 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 58,206 Reputation points
    2024-03-26T15:45:41.0266667+00:00

    SqlServer was based on the Sybase engine, where transact sql was a true language, and stored procedures are written in transact Sql. The language has cursors, if/then/else, loop, set, and can call other stored procs. you probably can migrate the code.

    Sqlserver's CLR integration is more similar to Oracle's stored procedures:

    https://learn.microsoft.com/en-us/sql/relational-databases/clr-integration/common-language-runtime-integration-overview?view=sql-server-ver16

    you might be interested in writing stored procedures in python:

    https://learn.microsoft.com/en-us/sql/machine-learning/sql-server-machine-learning-services?view=sql-server-ver16

    0 comments No comments

  2. Erland Sommarskog 102.5K Reputation points
    2024-03-27T22:40:35.5366667+00:00

    With the amount of information you have shared, the only answer that can be given is: Roll up you sleeves and start working!

    Rewriting procedural code into set-based is certainly not a walk in a park, as the mindsets are completely different. Those IF and ELSE typically translate to CASE expressions.

    One tip: while you want to avoid cursors and loops, you don't need to write one big monolith query. You can split up the work by using intermediate temp tables. The important thing is that you work with all data at once and not one-by-one.

    0 comments No comments