从pymssq中的存储过程返回第一个表后无法获取列名

2024-09-28 23:20:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在sql server中有一个返回多个表的过程:

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

在python中,光标.说明只返回第一个列名:a

我想得到每个表中的每个列名。在

我怎么能做到呢?在

这是我的代码:

^{pr2}$

Tags: 代码sqlserver过程ascreateselectend
1条回答
网友
1楼 · 发布于 2024-09-28 23:20:08

如果该命令返回多个表(即多个结果集)。您可以使用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

不强制结果集具有相同的列计数。例如:

^{pr2}$

结果是:

(('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)]

相关问题 更多 >