2011-04-20 104 views
6

我正在寻找一种方式,采取一个结果集,并用它来寻找在驻留在SQL Server 2008中的表中的记录 - 无需通过纺一次一个记录。用于查找记录的结果集数量可能会有数十万。到目前为止,我正在寻求使用sqlite3在内存中创建一个表,然后尝试将该表提供给一个存储过程,该过程需要一个表值参数。 SQL Server端的工作已经完成,用户定义的类型被创建,接受表值参数的测试过程已经存在,并且我已经通过TSQL对它进行了测试,看起来工作得很好。在Python中,通过sqlite3创建了一个简单的内存表。现在,catch,我发现用表值参数访问存储过程的唯一文档就是使用ADO.Net和VB,Python中没有任何内容。不幸的是,我还不足以让程序员翻译。有没有人使用SQL Server存储过程的表值参数?我应该考虑另一种方法吗?如何访问SQL Server 2008中存储过程的表值参数在Python

这里有一些链接:表值参数 体面的解释,以及如何设置它们在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 

现在艰难的部分 - 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 

好创建,我试图在通过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() 
+0

你为什么想要做的是Python? IronPython是一个选项吗?或者你仅限于CPython? – Achim 2011-04-20 19:47:31

+0

我们的主要供应商(ESRI)支持Python,他们为Python提供了一个插件,允许我们编写空间分析和地图生产脚本。我不确定arcpy会在IronPython中工作。如果是这样,那肯定是一个选择。我将检查ESRI是否支持IronPython。 – 2011-04-20 21:35:11

+0

ESRI不支持IronPython。 – 2011-04-21 15:14:49

回答

0

连接我以前从编码台湾居民入境许可证ADO.NET。 这里是经典ADO,我很感兴趣,台湾居民入境许可证sql server - Classic ADO and Table-Valued Parameters in Stored Procedure - Stack Overflow一个问题。它没有给出直接的答案,而是其他选择。

  • XML的选项比较容易,你可能已经考虑过了;它会需要更多的服务器端处理。
  • 这里是MSDN链接,台湾居民入境许可证的低水平ODBC编程。 Table-Valued Parameters (ODBC)。如果您可以切换到ODBC,则这是最接近的答案。
  • 你可以通过一个CSV字符串为nvarchar(最大),然后将它传递给CLR SplitString function,一个是快,但有默认的行为我不同意。

请回来后什么工作或不在这里。

相关问题