2011-04-20 105 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,一個是快,但有默認的行爲我不同意。

請回來後什麼工作或不在這裏。

相關問題