[英]How to access a SQL Server 2008 stored procedure with a table valued parameter in Python
I'm looking for a way to take a result set and use it to find records in a table that resides in SQL Server 2008 – without spinning through the records one at a time. 我正在寻找一种方法来获取结果集并使用它来查找驻留在SQL Server 2008中的表中的记录 - 而不是一次一个地旋转记录。 The result sets that will be used to find the records could number in the hundreds of thousands.
将用于查找记录的结果集可以达到数十万。 So far I am pursuing creating a table in memory using sqlite3 and then trying to feed that table to a stored procedure that takes a table valued parameter.
到目前为止,我正在尝试使用sqlite3在内存中创建一个表,然后尝试将该表提供给一个带有表值参数的存储过程。 The work on the SQL Server side is done, the user defined type is created, the test procedure accepting a table valued parameter exists and I've tested it through TSQL and it appears to work just fine.
完成SQL Server端的工作,创建用户定义的类型,接受表值参数的测试过程存在,并且我通过TSQL测试它,它似乎工作得很好。 In Python a simple in memory table was created through sqlite3.
在Python中,通过sqlite3创建了一个简单的内存表。 Now the catch, the only documentation I have found for accessing a stored procedure with a table valued parameter uses ADO.Net and VB, nothing in Python.
现在,我发现用于访问具有表值参数的存储过程的唯一文档使用ADO.Net和VB,在Python中没有任何内容。 Unfortunately, I'm not enough of a programmer to translate.
不幸的是,我还不够翻译程序员。 Has anyone used a SQL Server stored procedure with a table valued parameter?
有没有人使用带有表值参数的SQL Server存储过程? Is there another approach I should look into?
还有其他方法我应该研究一下吗?
Here are some links: Decent explanation of table valued parameters and how to set them up in SQL and using in .Net 以下是一些链接:表值参数的体面解释以及如何在SQL中设置它们并在.Net中使用
http://www.sqlteam.com/article/sql-server-2008-table-valued-parameters http://www.sqlteam.com/article/sql-server-2008-table-valued-parameters
http://msdn.microsoft.com/en-us/library/bb675163.aspx#Y2142 http://msdn.microsoft.com/en-us/library/bb675163.aspx#Y2142
Explanation of using ADO in Python – almost what I need, just need the structured parameter type. 在Python中使用ADO的解释 - 几乎我需要的,只需要结构化参数类型。 http://www.mayukhbose.com/python/ado/ado-command-3.php
http://www.mayukhbose.com/python/ado/ado-command-3.php
My simple code 我的简单代码
--TSQL to create type on SQL database
create Type PropIDList as Table
(Prop_Id BigInt primary key)
--TSQL to create stored procedure on SQL database. Note reference to
create procedure PropIDListTest @PIDList PropIDList READONLY
as
SET NOCOUNT ON
select * from
@PIDList p
SET NOCOUNT OFF
--TSQL to test objects.
--Declare variable as user defined type (table that has prop_id)
declare @pidlist as propidlist
--Populate variable
insert into @pidlist(prop_id)
values(1000)
insert into @pidlist(prop_id)
values(2000)
--Pass table variable to stored procedure
exec PropIDListTest @pidlist
Now the tough part – Python. 现在是艰难的部分 - Python。
Here is the code creating the in memory table 这是创建内存表的代码
import getopt, sys, string, os, tempfile, shutil
import _winreg,win32api, win32con
from win32com.client import Dispatch
from adoconstants import *
import sqlite3
conn1 = sqlite3.connect(':memory:')
c = conn1.cursor()
# Create table
c.execute('''create table PropList
(PropID bigint)''')
# Insert a row of data
c.execute("""insert into PropList
values (37921019)""")
# Save (commit) the changes
conn1.commit()
c.execute('select * from PropList order by propID')
# lets print out what we have to make sure it works
for row in c:
print row
Ok, my attempt at connecting through Python 好的,我尝试通过Python连接
conn = Dispatch('ADODB.Connection')
conn.ConnectionString = "Provider=sqloledb.1; Data Source=nt38; Integrated Security = SSPI;database=pubs"
conn.Open()
cmd = Dispatch('ADODB.Command')
cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "PropIDListTest @pidlist = ?"
param1 = cmd.CreateParameter('@PIDList', adUserDefined) # I “think” the parameter type is the key and yes it is most likely wrong here.
cmd.Parameters.Append(param1)
cmd.Parameters.Value = conn1 # Yeah, this is probably wrong as well
(rs, status) = cmd.Execute()
while not rs.EOF:
OutputName = rs.Fields("Prop_ID").Value.strip().upper()
print OutputName
rs.MoveNext()
rs.Close()
rs = None
conn.Close()
conn = None
# We can also close the cursor if we are done with it
c.close()
conn1.close()
I have coded TVPs from ADO.NET before. 我之前编写过ADO.NET的TVP。 Here is a question on TVPs in classic ADO that I am interested in, sql server - Classic ADO and Table-Valued Parameters in Stored Procedure - Stack Overflow .
以下是我感兴趣的经典ADO中的TVP问题, sql server - 存储过程中的经典ADO和表值参数 - 堆栈溢出 。 It does not give a direct answer but alternatives.
它没有给出直接答案,而是替代方案。
Please post back what works or does not here. 请回复有效或无效的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.