[DB-SIG] How to return results from stored procedures in Orac le
Sat, 31 Aug 2002 12:33:25 -0600
On Sat, 2002-08-31 at 10:10, Andy Todd wrote:
> Marcos S=E1nchez Provencio wrote:
> > Hello there.
> > In TSQL (the procedural language used in Sybase and MSSQL) it is very
> > simple to return rows from a procedure and use it as a query issued by
> > the user dinamically. I have managed to do get the same effect in
> > using a rather contrived mechanism which involved putting the
> > in a package and returning some type (cursor, I think).
> > How can I get it using DBAPI in Oracle? How about PostgreSQL?
> > What I want is something like
> > --TSQL code--
> > create procedure onlyone @id int
> > as
> > select * from mytable where id=3D@id
> > --END TSQL code--
> > So, for example, I can give permissions to access the data one by one,
> > knowing the id in advance.
> > _______________________________________________
> > DB-SIG maillist - DB-SIG@python.org
> > http://mail.python.org/mailman/listinfo/db-sig
> In Oracle, return a collection from your stored *function*
> In other words return a TABLE or a VARRAY from your stored function and=20
> see what happens.
Right. Or you can return a ref cursor.
> I haven't tried this using either of the DB-API modules for Oracle=20
> (DCOracle2 and cx_Oracle) so I can't confirm whether it works or not.
Both of these work in cx_Oracle. You do have to use non DB-API
constructs because the DB-API doesn't cover this very well. It seems to
cover returning a result set (and thus using fetchxxx() methods) but
does not handle arrays or complex data types at all.
> The reason I haven't tried it is because if I use stored procedures or=20
> functions I tend to make them pretty self contained and only return a=20
> status field. If you want to get a set of data into your Python program=20
> then just write a select statement and get the set using a cursor.
> By returning a set of data as a result of a stored function it would=20
> seem you are trying to dome some work in one place and the rest in=20
> another. If you start doing half of a transaction in a stored procedure=20
> and half in Python then only bad things can happen <0.5 wink>
Yes. That can become problematic. In addition, PL/SQL (Oracle's
programming language) has some serious limitations when compared with
Python. I just translated a complex program that I wrote a year or so
ago in PL/SQL to Python and the speedup involved ranges from 40% to
500%! If I had tried some complex combination of PL/SQL and Python I
would have run into some serious problems.
> Of course, YMMV.
> From the desk of Andrew J Todd esq - http://www.halfcooked.com
> DB-SIG maillist - DB-SIG@python.org