With newer versions of DB2 you can write stored procedures in SQL or you can create procedures in Java (or other languages) that can then be added to the database and called just like SQL procedures. I’m wondering what the the pros and cons of each method are. I’m specifically interested in comparing those two types of procedures, not debating procedures versus external code which I think has already been covered. Here is what I’ve come up with so far:
SQL:
-
Better performance for basic SQL functionality
-
Less verbose for simple logic, i.e. you can run SQL directly
-
No extra compile step – just create procedure …
Java:
-
More structured and full-featured code (classes, objects, reuse, libraries)
-
Better resources for help both in terms of programmers and documentation
Any other thoughts?
Not just Java but any procedural language: procedural, that’s the key.
DB2 is a relational algebra, not a programming language. If you need to do something complex, it’s better to use a procedural language than try to bend SQL into something it’s not meant to be.
We actually use REXX for our DB2/z work but I’ve seen guys here use bash on the mainframe for quick prototyping. Some who only use SQL create all these horrible temporary tables for storing things that are best kept in structures and away from the DB.
My thoughts are that complexity is the only differentiating factor between using SQL directly in a stored procedure and a procedural approach. We have two rules of thumb when making these decisions.
1/ If a unit of work requires more than two trips to the database, we make it a stored procedure.
2/ If a stored procedures creates a temporary table, then we use a procedural language to do it.