#How do you execute procedures in django

47 messages · Page 1 of 1 (latest)

carmine trout
#

i have this query

DECLARE @OutputMessage NVARCHAR(100)
                EXEC dbo.GEO_IBB_CF_PROCESS_F
                    @ENT = 'CUR',
                    @MON = 7,  -- Set to the previous month
                    @StatusMessage = @OutputMessage OUTPUT;
                SELECT @OutputMessage;

cant seem to execute query in python script but works in database
my code snippet:

from django.db import connection
sql = """
                DECLARE @OutputMessage NVARCHAR(100);
                EXEC dbo.GEO_IBB_CF_PROCESS_F
                    @ENT = %s,
                    @MON = %s,
                    @StatusMessage = @OutputMessage OUTPUT;
                SELECT @OutputMessage AS OutputMessage;
                """

            # Execute the SQL query
            logger.debug("Executing SQL query.")
            cursor.execute(sql, [ent, month])  # Pass parameters as a list

Thanks

rigid crest
carmine trout
#

is the query incomplete or the python code

#

query is complete but i just took a part of the python code

rigid crest
#

the python code

carmine trout
#

one sec

#
def execute_geo_ibb_cf_process(ent, month):
    """
    Executes the GEO_IBB_CF_PROCESS stored procedure based on the provided entity and month.
    Logs key actions and captures any errors.
    """
    print(ent)

    try:
        with connection.cursor() as cursor:
            # Define the output message variable
            output_message = ''
            logger.debug("Connection cursor initialized.")

            # Determine the appropriate SQL based on the entity
            if ent in ['BMW', 'BGEO', 'GBIBB']:
                sql = """
                DECLARE @OutputMessage NVARCHAR(100);
                EXEC dbo.GEO_IBB_CF_PROCESS_F_365
                    @ENT = %s,
                    @MON = %s,
                    @StatusMessage = @OutputMessage OUTPUT;
                SELECT @OutputMessage AS OutputMessage;
                """
            else:
                sql = """
                DECLARE @OutputMessage NVARCHAR(100);
                EXEC dbo.GEO_IBB_CF_PROCESS_F
                    @ENT = %s,
                    @MON = %s,
                    @StatusMessage = @OutputMessage OUTPUT;
                SELECT @OutputMessage AS OutputMessage;
                """

            # Execute the SQL query
            logger.debug("Executing SQL query.")
            cursor.execute(sql, [ent, month])  # Pass parameters as a list

            # Fetch the output message
            result = cursor.fetchone()
            if result:
                output_message = result[0]  # Fetch the value of `@OutputMessage`
                logger.info(f"Output Message: {output_message}")
            else:
                logger.warning("No output message returned.")

            return output_message

    except Exception as e:
        logger.error(f"Error executing stored procedure: {e}")
        print(e)
        raise
#

didnt wanna send the whole thing cause it'd look confusing haha

rigid crest
#

what is the error you get?

carmine trout
#

getting this error

No results. Previous SQL was not a query

rigid crest
#

is the error for cursor.ferchone()?

carmine trout
#

i dont think so

rigid crest
#

you can look at the traceback

carmine trout
#

i tried both cursor.fetchone() and cursor.fetchall()

rigid crest
#

or share it

carmine trout
#

ook

karmic yacht
#

Have you tried cursor.callproc?

carmine trout
#
No results.  Previous SQL was not a query.
ERROR Internal Server Error: /pre_standalone/alter_cashflow
ERROR "POST /pre_standalone/alter_cashflow HTTP/1.1" 500 76
#

this is the full traceback

carmine trout
carmine trout
#
ERROR Internal Server Error: /pre_standalone/alter_cashflow
ERROR "POST /pre_standalone/alter_cashflow HTTP/1.1" 500 76
INFO "POST /pre_standalone/curaco_cashflow/ HTTP/1.1" 200 69065
this is month: None
BGEO
INFO "GET /pre_standalone/fetch_CfMainDataGBIBB/ HTTP/1.1" 200 994
this is month: None
INFO "GET /pre_standalone/fetch_CfMainDataGBIBB/ HTTP/1.1" 200 994
BGEO
this is month: [2]
INFO "POST /pre_standalone/fetch_CfMainDataGBIBB/ HTTP/1.1" 200 66038
No results.  Previous SQL was not a query.
ERROR Internal Server Error: /pre_standalone/alter_cashflow
ERROR "POST /pre_standalone/alter_cashflow HTTP/1.1" 500 76
No results.  Previous SQL was not a query.
ERROR Internal Server Error: /pre_standalone/alter_cashflow
ERROR "POST /pre_standalone/alter_cashflow HTTP/1.1" 500 76
#

it just repeats

#

nothing special

rigid crest
carmine trout
#

ERROR "POST /pre_standalone/alter_cashflow HTTP/1.1" 500 76

#

this actually never runs succesfully

#

which is linked to the python script above

#

basically a function of alter cashflow runs first and then a function of showing that cashflow runs

rigid crest
carmine trout
#

ook

carmine trout
#

unless if you want me to do something else

rigid crest
carmine trout
#

ook

#
No active exception to reraise
INFO "GET /pre_standalone/curaco_cashflow/ HTTP/1.1" 200 994
ERROR Internal Server Error: /pre_standalone/alter_cashflow
ERROR "POST /pre_standalone/alter_cashflow HTTP/1.1" 500 64
rigid crest
#

and there was no message similar to this before?

#

you see what i don't understand is

you have an exception clause
but the logs you share don't show any sign of that

carmine trout
#

yea this confuses me too

#

should i try another method of executing this procedure

rigid crest
# carmine trout nope

i would put assert False right after that fetchone and see if it raises
or the old error is still in place

#

also, maybe delete the try/except for now and let it error out

sharp basin
#

I’ve only used stored procs from Java, but I remember having to consume update counts from the connection before it would return the actual result set I needed.
Is it a similar thing going on here?

#

You could try checking .rowcount or calling .nextset() while fetchone is returning None?

carmine trout
#

i solved the issue thank you everyone

#

apparently pyodbc cant execute the Declare @OutputMessage NVARCHAR(100) and SELECT @OutputMessage AS OutputMessage; so i just got rid of it and set a custom status message

DECLARE @OutputMessage NVARCHAR(100);
                EXEC dbo.GEO_IBB_CF_PROCESS_F_365
                    @ENT = %s,
                    @MON = %s,
                    @StatusMessage = @OutputMessage OUTPUT;
                SELECT @OutputMessage AS OutputMessage;