如何使用Python中的表值参数访问SQL Server 2008存储过程

Jil*_*l H 6 python ado.net stored-procedures sql-server-2008

我正在寻找一种方法来获取结果集并使用它来查找驻留在SQL Server 2008中的表中的记录 - 而不是一次一个地旋转记录.将用于查找记录的结果集可以达到数十万.到目前为止,我正在尝试使用sqlite3在内存中创建一个表,然后尝试将该表提供给一个带有表值参数的存储过程.完成了SQL Server端的工作,创建了用户定义的类型,接受表值参数的测试过程存在,我通过TSQL对其进行了测试,看起来效果很好.在Python中,通过sqlite3创建了一个简单的内存表.现在,我发现用于访问具有表值参数的存储过程的唯一文档使用ADO.Net和VB,在Python中没有任何内容.不幸的是,我还不够翻译程序员.有没有人使用带有表值参数的SQL Server存储过程?还有其他方法我应该研究一下吗?

以下是一些链接:表值参数的体面解释以及如何在SQL中设置它们并在.Net中使用

http://www.sqlteam.com/article/sql-server-2008-table-valued-parameters

http://msdn.microsoft.com/en-us/library/bb675163.aspx#Y2142

在Python中使用ADO的解释 - 几乎我需要的,只需要结构化参数类型. http://www.mayukhbose.com/python/ado/ado-command-3.php

我的简单代码

--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
Run Code Online (Sandbox Code Playgroud)

现在是艰难的部分 - Python.

这是创建内存表的代码

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
Run Code Online (Sandbox Code Playgroud)

好的,我尝试通过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()
Run Code Online (Sandbox Code Playgroud)

小智 0

我以前曾从 ADO.NET 编写过 TVP。这是我感兴趣的关于经典 ADO 中的 TVP 的问题,sql server - 经典 ADO 和存储过程中的表值参数 - 堆栈内存溢出 它没有给出直接答案,而是给出替代方案。

  • XML 的选择更容易,您可能已经考虑过它;这将需要更多的服务器端处理。
  • 这是 TVP 低级 ODBC 编程的 msdn 链接。 表值参数 (ODBC)。如果您可以切换到 ODBC,这是最接近的答案。
  • 您可以将 csv 字符串传递给 nvarchar(max),然后将其传递给CLR SplitString 函数,该函数速度很快,但具有我不同意的默认行为。

请在这里发回有效或无效的内容。