2017-12-27 151 views
4

从存储过程的第一个表后回我在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