2017-12-27 8 views
4

のストアドプロシージャからの最初のテーブルの復帰後にカラム名を取得できません:Pythonでは、私は複数のテーブルを返し、SQL Serverの手順持っpymssql

create procedure newtest 
as 
begin 
    select 1 as a 
    select 2 as b 
end 

を、cursor.descriptionはちょうど最初の列名を返します。

各テーブルのすべての列名を取得したいと考えています。

どうすればいいですか?コマンドは、複数のテーブル(すなわち、複数の結果セット)を返す場合

cur.execute(com) 
       num_tables=0 
       while True: 
        print(cur.description) 
        ret=cur.fetchall() 

        if len(ret)>0: 
         ret_list.append(ret) 
         num_tables+=1 
         print(ret)      
        else: 
         break 
+0

無応答? –

答えて

3

これは私のコードです。 Cursor.nextset()を使用して、あるセットから次のセットに切り替えることができます。

のようなもの:結果セットが同じ列数を持つことを余儀なくされていない

num_tables = 0 
while True: 
    print(cur.description) 

    # all lines of the current set 
    ret = cur.fetchall() 

    if len(ret) > 0: 
     ret_list.append(ret) 
     num_tables += 1 
     print(ret) 

    # check and fetch the next set 
    if not cur.nextset(): 
     break 

。たとえばで:

create procedure newtest 
as 
begin 
    select 1 as a 
    select 2 as b, 3 as c 
end 

結果は次のとおりです。まだ

(('a', <class 'int'>, None, 10, 10, 0, False),) 
[(1,)] 
(('b', <class 'int'>, None, 10, 10, 0, False), ('c', <class 'int'>, None, 10, 10, 0, False)) 
[(2, 3)] 
+0

私はcur.connection._conn.get_header()を使用しましたが、返されるテーブルはすべて返されます。 –

+0

可能であれば、ドライバ特有のまたは文書化されていないものの代わりに標準(PEP 249)関数を使用することをお勧めします – bwt

関連する問題