toadi has asked for the wisdom of the Perl Monks concerning the following question:

Hello, it's rather a long time that I've been here...

I wrote some PL/SQL procedures. When they're finished I Need to run some queries on newly created tables.

This needs to be done every monday. I thought maybe I can write some nifty perl script.

My question is: can PL/sql code be implemented in perl. Is there somewhere documentation about it ???
Can some guide me to the light?

My opinions may have changed,
but not the fact that I am right

Replies are listed 'Best First'.
Re: PL/SQL code and Perl
by agoth (Chaplain) on Jan 22, 2001 at 14:34 UTC
    You can, its easy, there are a couple of brief examples in the DBI::FAQ module, but just faff about until you get it going.

    The code below uses execute to do an already compiled procedure but the DBI::FAQ shows use of begin from Perl which may be more use.

    Code shows proc and code to kick it off.

    CREATE OR REPLACE PROCEDURE my_proc (id IN NUMBER, uname IN VARCHAR2, card IN VARCHAR2) AS BEGIN INSERT INTO TABLE VALUES (id, uname, card); END; my $sth = $dbh->prepare('call owner.my_proc(?, ?, ?)'); for (1..$iter) { print "$_\n"; $sth->execute($id, $name, $card); $id++; } $dbh->commit;
Re: PL/SQL code and Perl
by eg (Friar) on Jan 22, 2001 at 13:43 UTC

    What do you mean by "can PL/SQL code be implemented in perl"? Do you mean can you make a perl-ish translation of whatever your PL/SQL procedure does (most likely yes) or do you mean can you put your PL/SQL procedure's code in perl have have perl set it up and execute it? (again yes.)

    The DBD::Oracle perldoc has PL/SQL examples of the latter.

    Personally, I prefer to have the Oracle DBA be responsible for loading the PL/SQL procedure into the database. Then I can easily call it from perl without having to ever worry about it. That way if anything goes wrong, I can blame the DBA :)

Re: PL/SQL code and Perl
by mwp (Hermit) on Jan 22, 2001 at 13:47 UTC

    By "PL/SQL" I'm assuming you're using Oracle. AFAIK, there's no (existing) way to implement Oracle's SQL extensions through DBI, although you have a couple options:

    1. Write a stored procedure that does what you need and returns some status data in Oracle, then call that stored procedure from Perl DBI.
    2. Attempt to access the unique Oracle commands through DBI and DBI::func() (which can be used to call driver-specific functions). However, the functionality you need will have to be built into DBD::Oracle. Check the manpage.
    3. Track down OraPerl and see if you can use that, although from what I've heard no one uses that now. Read this, maybe.

    If nothing else, hit Google and start searching like mad. =)

    Disclaimer: I've only worked Oracle as a programmer, not a DBA, and we weren't doing anything outside of your usual SELECT, INSERT, etc. So it's possible that I am missing the whole point, here.

    Update: After perusing around for a bit, it appears that you can indeed use PL/SQL with DBI. You just prepare a query with the proprietary commands and send that through DBI to Oracle. Works the same as a standard query. I'm impressed. {g}