如何在 Jupyter Notebook 中使用 Pyodbc 从 SQL 服务器存储过程中检索多行
How to retrieve multiple rows from a SQL Server stored procedure using Pyodbc in Jupyter Notebook
我在 SQL 服务器中有一个存储过程,它接受 3 个输入参数并可以生成多行作为输出。事实上,在我当前的案例中,它返回了 20 行。例如,如果我从 SSMS 手动执行存储过程,我将分别得到以下代码和部分输出:
代码:
DECLARE @return_value int
EXEC @return_value = [Coverage-Source].[ReadCoverageMapping]
@client = N'Capital BlueCross',
@lineOfBusiness = N'Commercial',
@distributionChannel = N'Retail'
SELECT 'Return Value' = @return_value
GO
输出:
ID Attribute Value CoverageName
-----------------------------------------
1 Copay Yes Retail Base
2 Copay No Retail
. . . .
. . . .
Return Value
20
现在,当我尝试使用 pyodbc 从 jupyter notebook 读取存储过程时,出现错误
Procedure or function ReadCoverageMapping has too many arguments specified
我想要这样的输出:
ID Attribute Value CoverageName
------------------------------------------
1 Copay Yes Retail Base
2 Copay No Retail
. . . .
. . . .
我试过这段代码:
client = 'Capital BlueCross'
lineOfBusiness = 'Commercial'
distributionChannel = 'Retail'
cnxn = pyodbc.connect(r'Driver={SQL
Server};Server=MyServer;Database=COV_SRCE_TEST;Trusted_Connection=yes;')
sql = """\
DECLARE @out nvarchar(max);
EXEC [cov_srce_test].[coverage-source].ReadCoverageMapping @client = ?, @lineOfBusiness = ?,
@distributionChannel = ?, @param_out = @out OUTPUT;
SELECT @out AS the_output;
"""
values = (client, lineOfBusiness, distributionChannel)
cnxn.execute(sql, values)
rows = cnxn.fetchall()
while rows:
print(rows)
if cnxn.nextset():
rows = cnxn.fetchall()
else:
rows = None
有什么办法可以实现吗?我尝试了多种方法,但找不到解决方案。
那些批次不同。看起来应该是:
sql = """\
DECLARE @return_value int
EXEC @return_value = [Coverage-Source].[ReadCoverageMapping]
@client = ?,
@lineOfBusiness = ?,
@distributionChannel = ?
SELECT 'Return Value' = @return_value
"""
这将输出两个结果集,一个来自存储过程,另一个用于 Return value
,您可以忽略它。
我在 SQL 服务器中有一个存储过程,它接受 3 个输入参数并可以生成多行作为输出。事实上,在我当前的案例中,它返回了 20 行。例如,如果我从 SSMS 手动执行存储过程,我将分别得到以下代码和部分输出:
代码:
DECLARE @return_value int
EXEC @return_value = [Coverage-Source].[ReadCoverageMapping]
@client = N'Capital BlueCross',
@lineOfBusiness = N'Commercial',
@distributionChannel = N'Retail'
SELECT 'Return Value' = @return_value
GO
输出:
ID Attribute Value CoverageName
-----------------------------------------
1 Copay Yes Retail Base
2 Copay No Retail
. . . .
. . . .
Return Value
20
现在,当我尝试使用 pyodbc 从 jupyter notebook 读取存储过程时,出现错误
Procedure or function ReadCoverageMapping has too many arguments specified
我想要这样的输出:
ID Attribute Value CoverageName
------------------------------------------
1 Copay Yes Retail Base
2 Copay No Retail
. . . .
. . . .
我试过这段代码:
client = 'Capital BlueCross'
lineOfBusiness = 'Commercial'
distributionChannel = 'Retail'
cnxn = pyodbc.connect(r'Driver={SQL
Server};Server=MyServer;Database=COV_SRCE_TEST;Trusted_Connection=yes;')
sql = """\
DECLARE @out nvarchar(max);
EXEC [cov_srce_test].[coverage-source].ReadCoverageMapping @client = ?, @lineOfBusiness = ?,
@distributionChannel = ?, @param_out = @out OUTPUT;
SELECT @out AS the_output;
"""
values = (client, lineOfBusiness, distributionChannel)
cnxn.execute(sql, values)
rows = cnxn.fetchall()
while rows:
print(rows)
if cnxn.nextset():
rows = cnxn.fetchall()
else:
rows = None
有什么办法可以实现吗?我尝试了多种方法,但找不到解决方案。
那些批次不同。看起来应该是:
sql = """\
DECLARE @return_value int
EXEC @return_value = [Coverage-Source].[ReadCoverageMapping]
@client = ?,
@lineOfBusiness = ?,
@distributionChannel = ?
SELECT 'Return Value' = @return_value
"""
这将输出两个结果集,一个来自存储过程,另一个用于 Return value
,您可以忽略它。