TheYoungMonk has asked for the wisdom of the Perl Monks concerning the following question:
Is there any way to do it?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing Arrays from Perl to Stored Procedures
by zby (Vicar) on Mar 07, 2003 at 09:04 UTC | |
Calling stored procedures is currently not defined by the DBI. Some drivers, such as DBD::Oracle, support it in non-portable ways. See driver documentation for more details.)Sometimes you can call your procedure as a function in a select statement: select StoredFunc(?) from dual. And bind the parameter as usual. I don't know all the libraries but I believe in most there is no method to pass an generic array as a parameter. Of course - if you are using PostreSQL stored procedures in perl than you can pass parameters as usuall. | [reply] [d/l] |
by TheYoungMonk (Sexton) on Mar 07, 2003 at 09:46 UTC | |
But if at all there is no method to pass arrays to a SP, is there no other alternative way which can solve the problem mentioned in an optimized manner ? how about any obfuscations which can aim at this...curious.. Expecting some kind of enlightenment from the monk community to resolve this issue... | [reply] |
by iguanodon (Priest) on Mar 07, 2003 at 10:58 UTC | |
Note that the default implementation currently only supports non-data returning statements.If you want to get data back from Oracle stored procedures, you need to call bind_param_inout but this method doesn't take an array as an argument. If the values you want to pass are in an array, just pass the array elements. | [reply] |
by zby (Vicar) on Mar 07, 2003 at 10:12 UTC | |
Update: I've overlooked that you need to call the procedure to do the additionall computation. | [reply] |
|
Re: Passing Arrays from Perl to Stored Procedures
by BrowserUk (Patriarch) on Mar 07, 2003 at 14:31 UTC | |
I know little about DBI and even less about Oracle, but I just came across DBIx::Procedure::Oracle on CPAN which has this description: DBIx::Procedure::Oracle - Call PL/SQL stored procedures and functions without writing SQL or needing to know about data types or bindings. That said, it goes on to talk about using positional parameters (:n) and not named parameters. It queries the definition of the stored procedure from the DB itself and then uses that to decide how many and what type the parameters are. So, it would seem to me that the problem you have is how to define a stored procedure in Oracle to accept a large number of values as arguments without needing to name or number them all indivually? That's very much an Oracle question not a Perl one. Taking a quick look at the pod for DBD::Oracle (*This has a few PL/SQL examples that might be illuminating for you.) and trying to extrapolate from my long unused DB2 knowledge, I seem to recall that the usual way to handle the type of processing that you are talking about is to do a standard INSERT of the data records into seperate table that is either created on the fly or is defined simply for the purpose of receiving the raw data, and then call the stored procedure to process the raw data from that table into the real tables, possibly removing it from the raw storage table as it goes. That way, the large volumes of values are bound to the INSERT statement in the normal way, and then the procedure can be invoked with just the name of the TABLE that it is to process. As I said at the top, I am on really shaky ground with this stuff, but it really sounds like your problem is one of how to define a stored procedure to accept large numbers of parameters in a convenient way--an Oracle/SQL problem-- rather than perl one. Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong. 2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible 3) Any sufficiently advanced technology is indistinguishable from magic. Arthur C. Clarke. | [reply] |
|
Re: Passing Arrays from Perl to Stored Procedures
by Abigail-II (Bishop) on Mar 07, 2003 at 07:06 UTC | |
Could you tell us some details of what you want to do? Abigail | [reply] |
by TheYoungMonk (Sexton) on Mar 07, 2003 at 08:17 UTC | |
The perl program i am working on is supposed to read a file containing some comma-separated values and load into a table. Each line of the file containing the comma-separated values represent one record in a table.(there will be nearly 100 such lines). There is a stored procedure which has certain computations and finally inserts the values into the table. I need to call this stored procedure from the perl program and pass it the records. thot of storing the Comma-separated values of each record into an array. But as u pointed out, if arrays cannot be passed to stored procedure...then how to go about doing it?? I hope the experience of the monks will help me clear this block...quite urgent, so plz share ur knowledge in this area. | [reply] |
by SheridanCat (Pilgrim) on Mar 07, 2003 at 15:27 UTC | |
How would the stored procedure know how to unpack the array? Perhaps you could do some sort of binary packing that could then be unpacked by the stored proc, but that's not the kind of thing I'd use a stored procedure for. My suggestion is to feed each record to the stored procedure individually. This is a pretty common thing to do. By the way, 100 records is nothing. That's test data where I come from. When you start talking in millions and portions of millions things get interesting. Also, it seems to me that stuffing everything into an array, passing that array to the proc and having the proc process each record in the array and then insert it is very, very similar to letting perl unpack the array and pass in each record to the proc for further processing and insertion. I'm sure this isn't particularly helpful, but may shed some light on a solution for you. Regards, Cat | [reply] |
by Abigail-II (Bishop) on Mar 07, 2003 at 22:10 UTC | |
But I wouldn't do it that way. It's so.... not SQL-ish. I'd use the database's bulk copy feature to read in the CSV file into a temporary table. Then I'd modify the stored procedure to read the data from the temp table and insert it into the real table. No Perl needed, unless the database isn't able to read in a CSV file (most databases can). In which case I'd write a simple DBI program, using DBI::CVS to read in the CVS file, and DBI::whatever to write it to the temp table. Abigail | [reply] |
|
Re: Passing Arrays from Perl to Stored Procedures
by poj (Abbot) on Mar 07, 2003 at 10:31 UTC | |
poj | [reply] [d/l] |
by poj (Abbot) on Mar 07, 2003 at 11:30 UTC | |
poj | [reply] [d/l] |
by TheYoungMonk (Sexton) on Mar 07, 2003 at 12:46 UTC | |
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! | [reply] |
by dragonchild (Archbishop) on Mar 07, 2003 at 15:51 UTC | |
by Anonymous Monk on Mar 07, 2003 at 21:44 UTC | |
by TheYoungMonk (Sexton) on Mar 07, 2003 at 10:58 UTC | |
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... | [reply] |
by Anonymous Monk on Mar 08, 2003 at 09:18 UTC | |
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. | [reply] [d/l] |
|
Re: Passing Arrays from Perl to Stored Procedures
by OM_Zen (Scribe) on Mar 07, 2003 at 18:05 UTC | |
The backend stored procedure , HOW can a stored procedure accept an array first .You are thinking of passing an array to stored procedure , but how iwll a stored procedure accept it The procedure written like
Hence trying to pass an array to a stored procedure which can never accept an array is not how it could be working That you could do is concatenate the scalar values into a single scalar value in perl and then send it to the Orcale stored procedure and then parse it with substr inside the oracle procedur and use it like
The stored procedure has to
The idea of sending arrays to stored procedures is not that good in that stored procedures , I mean the databases do not have these array only the table and record type , mya be try using those | [reply] [d/l] [select] |
|
Re: Passing Arrays from Perl to Stored Procedures
by Anonymous Monk on May 14, 2012 at 21:06 UTC | |
Passing and getting array parameters/return values If we want to pass or return arrays, DBI binding does not support it directly. However, there is a trick how we can do it, both for input parameters, and return/output parameters. This is basically idea: a) For output parameters, we use reference cursor instead of directly returning the array ( or any other collection). We will show an example for array, but you can easily make it for associative array. b) For input parameters, we do not try to bind them directly. Instead, we loop through Perl array/hash, and create a string, which can be used in PL/SQL Perl block string. So we simply call the PL SQL procedure/function directly. Example: We define the type STRING_TABLE on the schema level, in the schema TAXNET:
Input array parameter: The example shows what we need to do:
Output array parameter/returned value: We see how we define the cursor to return the collection. We cannot define cursor directly with associative array, but we can “copy” the associative array to the nested table type variable, and after that we can define the cursor with nested table elements. In the function above, we can change inputList to bean associative array too. Now it is easy to get the reference cursor in Perl. We did it in the stoed function above, and this is the line:
| [reply] [d/l] [select] |
by Anonymous Monk on May 14, 2012 at 21:12 UTC | |
Re: Passing Arrays from Perl to Stored Procedures by Anonymous Monk on May 14, 2012 at 21:06 UTC This article is created by Ratimir Vukicevic - Sorry, did not pay attention how to put my "signature". Anyway the name does not matter, only if I need it for work reference somewhere ... ( hopefully not ) | [reply] |