简体   繁体   中英

How can I create stored procedure from within Python script?

I am looking to way to create an oracle stored procedure in Python.

I know, cursor.callproc in cx_Oracle exists to call the existing stored procedure, but I am looking for a way/method to create an Oracle stored procedure in Python.

Just use execute() with the SQL CREATE OR REPLACE command.

To check if the procedure was valid, you need to query a base table like user_errors .

with connection.cursor() as cursor:
    cursor.execute("""create or replace procedure x(a in number) as begin BOGUS end;""")
    cursor.execute("""
            select name, type, line, position, text
            from user_errors
            where name = 'X'
            order by name, type, line, position""")
    data = cursor.fetchall()
    print(data) 

The output is:

[('X', 'PROCEDURE', 1, 41, 'PLS-00103: Encountered the symbol "END" when expecting one of the following:\n\n   := . ( @ % ;\nThe symbol ";" was substituted for "END" to continue.\n')]

We are looking at how a future version of cx_Oracle can directly expose Oracle's 'success with info' flag and the compilation error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM