2

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.

2
  • What did you try or research? Building stored procedures is just running SQL or plsql code which you can run in Python or any database connected language (Java, PHP, C#, etc.). Commented Sep 22, 2020 at 0:02
  • What should I do when someone answers my question? Commented Sep 22, 2020 at 22:10

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This does not seem to work when its a multiline procedure ?
Works for me :) Did you use triple quotes? Open a new question with behavior details if you still have a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.