in reply to Passing Arrays from Perl to Stored Procedures

This example is in the Oracle::DBD
# Example 2 # # Now we call a procedure that has 1 IN parameter. # Here we use bind_param # to bind out parameter to the prepared statement just # like you might # do for an INSERT, UPDATE, DELETE, or SELECT statement. # # I could have used positional placeholders # (e.g. :1, :2, etc.) or # ODBC style placeholders (e.g. ?), but I prefer # Oracle's named # placeholders (but few DBI drivers support them so # they're not portable). my $err_code = -20001; $csr = $db->prepare(q{ BEGIN PLSQL_EXAMPLE.PROC_IN(:err_code); END; }); $csr->bind_param(":err_code", $err_code); # PROC_IN will RAISE_APPLICATION_ERROR which will # cause the execute to 'fail'. # Because we set RaiseError, the DBI will croak (die) # so we catch that with eval. eval { $csr->execute; }; print 'After proc_in: $@=',"'$@', errstr=$DBI::errstr, ret_val=$ret_ +val\n";
poj

Replies are listed 'Best First'.
Re: Re: Passing Arrays from Perl to Stored Procedures
by poj (Abbot) on Mar 07, 2003 at 11:30 UTC
    Substitute your own parameters like this
    # @parameter is the array with 3 parameters $csr = $db->prepare(q{ BEGIN SQL_PROC_NAME(:1,:2,:3); END; }); $csr->bind_param(":1", $parameter[0] ); $csr->bind_param(":2", $parameter[1] ); $csr->bind_param(":3", $parameter[2] ); eval { $csr->execute; };
    poj
      If i use individual values instead of a complete array, this will mean i'll have to run this code and execute the procedure for each record insertion, which is be highly inefficient when no. of records is some 100!!

      Instead, what i am looking for is parsing the CSV and getting all records into an array and passing it in one-go to the Stored procedure(HOW?) which will then loop through it and insert accordingly

      ...anyway, thanx!

        If you use prepare_cached(), it will be quite faster than you think.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        As was stated before, 100 records to a database is NOTHING.

        DBD::Oracle apparently does not support passing whole arrays for binding, if it did I'm sure there would be some code behind it that broke the array down to individual elements and bind each of them. So I would suggest something that once you have your statement prepared pass it to a sub like this (untested btw)...

        sub BindValues { my $csr = shift; my $array_ref = shift; my $i; my $placeholder; foreach $i ( 0 .. $#$array_ref ) { $placeholder = ":" . $i; $csr->bind_param($placeholder, $array_ref->[$i]); } }

        Call it like this... BindValues( $csr, \@CSVArray );

        I don't have much experience with Perl and databases, I have some DBD::Oracle experience, but I have more experience in other languages and have yet to see bind parms be anything other than individual elements.

        Like the other post under this one I'm replying to says, prepare_cached makes things much faster and you will find that its not very inefficient at all and that the sub that breaks down your array and individually binds the items will run quite quickly.

Re: Re: Passing Arrays from Perl to Stored Procedures
by TheYoungMonk (Sexton) on Mar 07, 2003 at 10:58 UTC
    Thanks for the code posted!...but how is it relevant to the problem at hand ?

    What is required is the same kind of Stored Procedure parameter passing to be done, but when the parameter is an array of values.

    All new ideas welcome...

      Ok your probally doing something like this:

      open (CSVFILE, input.csv); while (!eof(CSVFILE)) { $currLine = <CSVFILE>; split /\,/, $currLine, @curline; dbi_sub (@currline); }


      where dbi_sub will feed that to your stored proc. Now as said before I would just have oracle do this make a PL/SQL function that takes a single varchar(255) as a parameter, being the name of the textfile and read it in. But another method would be to pass the $currLine that contains a string of parameters seperated by commas and have your stored procedure parse it. But you never answered the $25,000 question, why aren't you having oracle do this for you. It can and more effecietly than perl probally, but then again the great stored procedures debate is something for the grand council of DB admin and RDMS coder wizards to debate.