Thursday, 8 September 2011

Execute REALLY BIG SQL statements with DBMS_SQL

Question:
I have been assigned the task of automating a data warehouse load procedure with PL/SQL. I'm having trouble with one part: creating views.
We have some procedures that read link tables and from them generate the DDL to create views. The DDL is written to OS files via UTL_FILE. They are then applied by going into SQL*Plus and doing a START os_file.
I would like to be able to do everything I need inside PL/SQL and not have to resort to calling a back-end processor to do the view creation. I can do DDL with EXECUTE IMMEDIATE so had hoped I could do a start also, but no such luck! I have already written a back-end processor that we use for other stuff (using pipes), but had hoped that there might be something in Oracle Database 10g (which we are migrating to from 8i) that would allow me to use PL/SQL instead. Some views are over 32KB in length, so I can't read them in and process them that way.
Answer:
Michael, you are on the right track! You have large, complex CREATE VIEW statements in files. You now want to run that file from within PL/SQL and create the view. You have tried using EXECUTE IMMEDIATE, but those statements sometimes exceed 32KB in length. DBMS_SQL to the rescue!
DBMS_SQL is the "old" way of doing dynamic SQL, but for some requirements, it remains the best or only option. For example, if you have a very long SQL statement (greater than 32KB in length), you cannot use EXECUTE IMMEDIATE (the new, slick, easy native dynamic SQL statement).
Instead, you can take advantage of an overloading of DBMS_SQL.PARSE that accepts an array of strings and parses the SQL statement that is constructed from the rows of that array. In the Downloads section you will find a program, compile_from_file, that demonstrates this capability.
This program reads the contents of a file with UTL_FILE, loads each line into a collection whose type is defined in the DBMS_SQL package, and then executes the statement. Note that I read the contents of the file into a collection defined on the DBMS_SQL.VARCHAR2A type, which was introduced in Oracle Database 10g Release 1. If you are using an earlier version of the Oracle Database, use the DBMS_SQL.VARCHAR2S type instead.

2 comments:

  1. Where is the dowload section? I would like to see this example. Thanks

    ReplyDelete
  2. Sorry Friend , there is no download section.

    you can write your own sql statement and try that.

    ReplyDelete