I have a stored procedure I have to work with. From SQL developer is gives results that look like a table. But when I try to get the results with Pyodbc, I only get:
Error: No results. Previous SQL was not a query.
I think it's how the results are being produced maybe, because it's using select. I'm not sure how to get the output. I've tried a few other ideas from guides and SO questions, but I've had no luck.
The SP looks like this:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE schema.spname
AS
BEGIN
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
BEGIN TRY
DECLARE @NewLogID BIGINT
DECLARE @recs INT = 0
DECLARE @Process VARCHAR(200) = OBJECT_NAME(@@PROCID)
-- Log the Process
EXEC anrm.WriteANRMLog @ProcessName = @Process,@ReturnLogID=@NewLogID OUTPUT
SELECT receipt_id, order_id, user_id
FROM tableA
WHERE A.Qty>0
AND datesent IS NULL
--EXEC schema.othertable @RecordCount = @recs,@logid= @NewLogID, @ReturnLogID=@NewLogID OUTPUT
PRINT @Process + ' - Completed'
END TRY
BEGIN CATCH
EXEC anrm.PrintError
--EXEC anrm.LogNRMError
END CATCH
END
And I'm trying to get the results, most recently, like this.
try:
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=' +
server+';DATABASE='+database+';UID='+username+';PWD=' + password)
cnxn.autocommit = True
cursor = cnxn.cursor()
cursor.execute("SET NOCOUNT ON; exec schema.spname")
row = cursor.fetchone()
while row:
print(str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
cursor.close()
del cursor
cnxn.close()
except Exception as e:
print("Error: %s" % e)
Can you modify the SP? If so, instead of running the select in the SP, have it insert results into a temp table. Then in python
cursor.execute("exec schema.spname")
cursor.execute("select * from #my_temp_table")
cursor.execute("drop table #my_temp_table")
Does this work for you?
# SQL Server format
cursor.execute("exec sp_dosomething(123, 'abc')")
# ODBC format
cursor.execute("{call sp_dosomething(123, 'abc')}")
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.