I have Azure serverless functions which are connecting to a SQL database. One of the functions executes a stored procedure which in turn executes sub procedures, each of which produce a results set.
in T-SQL:
CREATE PROCEDURE usp_proc_1 AS
SELECT GETDATE() AS 'Date'
GO
CREATE PROCEDURE usp_proc_2 AS
EXEC usp_proc_1
SELECT 1 AS 'number'
GO
EXEC usp_proc_2
In Python I have as helper function that I have cribbed from other posts,
def getMultiResults(conn , query, params = None):
# use this when the query returns many results sets
cursor = conn.cursor()
records = []
if (params == None):
cursor.execute(query)
else:
cursor.execute(query,params)
# first record set
logging.info(cursor.description[0])
attributes = [key[0] for key in cursor.description] # gets the column headers
dataframe = cursor.fetchall()
cursor.commit()
records.append( [dict(zip(attributes, row)) for row in dataframe])
# more record sets
while (cursor.nextset()):
logging.info(cursor.description[0])
attributes = [key[0] for key in cursor.description] # gets the column headers
dataframe = cursor.fetchall()
cursor.commit()
records.append( [dict(zip(attributes, row)) for row in dataframe])
return records
When I call the getMultiResults on EXEC usp_proc_2 the dataframe only contains the date results set. I think this is because although the outer procedure looks like it returns two data sets, the inner procedure creates the first one and this is all that the fetchall() is seeing. if the stored procedure was
CREATE PROCEDURE usp_proc_3
SELECT GETDATE() AS 'Date'
SELECT 1 AS 'Number'
GO
EXEC usp_proc_3
then fetchall() would work as expected
Am I correct and if so, how can I correct the issue.
In reality I have a complex master procedure that depending on the business scenario runs a number of different sub-procedures, each of which returns both a results set and a return value. All I need is the LAST results set from the master procedure but this could be the 3rd, 5th or 6th results set depending on what internal procedures were run.