0

SQL server has the prompt: sp_help table_name.

I would like to access the output of this using django.

I am using django 2.1. I expected the following code to work, but it only returns the first part of what SQL server returns.

cursor = connection.cursor()
cursor.execute("sp_help testTable")
print(cursor.fetchall())

I expected the output to be:

[('testTable', 'dbo', 'user table', datetime.datetime(2019, 10, 14, 16, 0, 46, 700000))]
[('testCol', 'varchar', 'no', '50', '', '', 'no', 'no', 'no', 'SQL_Latin1_General_CP1_CI_AS')]
[...more result sets...]

Instead I got:

[('testTable', 'dbo', 'user table', datetime.datetime(2019, 10, 14, 16, 0, 46, 700000))]
Uuuuuumm
  • 608
  • 5
  • 21
  • You got the first result set returned from that procedure. I don't know anything about django but it is because there are multiple result sets returned, not multiple rows with varying numbers of columns. – Sean Lange Nov 04 '19 at 21:33
  • Right. I would like to get all the result sets. It seems like fetchall() does the same thing as fetchone(). – Uuuuuumm Nov 08 '19 at 14:34
  • 1
    Not quite. fetchone() retrieves the next row from the active dataset, fetchall() retrieves all the rows from the active dataset. The procedure sp_help returns **multiple datasets**. Seems like this is the solution for dealing with this in python. https://stackoverflow.com/questions/273203/access-second-result-set-of-stored-procedure-with-sql-or-other-work-around-pyth – Sean Lange Nov 08 '19 at 14:38

1 Answers1

0

Thanks to the guidance of Sean Lange, I figured out my problem was not django related. I needed to use: 'cursor.nextset()' to access the other result sets.

   cursor.nextset()
Uuuuuumm
  • 608
  • 5
  • 21