简体   繁体   中英

Pyodbc - read output parameter of stored procedure (SQL Server)

I'm trying to get the output parameter of an SQL Server stored procedure using pyodbc. The procedure is located in SQL Server and I can insert data successfully from my program. This is part of the SP:

INSERT INTO Table(a,b,c,d,e,f,g) 
VALUES (@a,@b,@c,@d,@e,@f,@g);
SET @Result = 'Inserted'
RETURN @Result

When I try to read the result variable in my code it shows empty.

This is the call from python, I used pyodbc:

        cursor = self.db.cursor()

        sql = """\
        DECLARE @out nvarchar(max);
        EXEC [dbo].[storedProcedure] @x = ?, @y = ?, @z = ?,@param_out = @out OUTPUT;
        SELECT @out AS the_output;
        """

        cursor.execute(sql, (x, y, z))
        row = cursor.fetchone()
        print(row[0])

Then create a stored procedure with an output parameter.

The above answer from Printthis worked and thanks a lot, yes i also added conn.commit() after:

row = cursor.fetchone() 
token=row[0]
conn.commit()

if i put commit before fetchone, i would get an error, sequence error sqlfetch...

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