2017-08-29 88 views
1

我有一個名爲sqlQueries的列的Excel工作表。每個單元都有一組運行的查詢。如何使用單個對象一次性在qtp中運行多個查詢

我能夠使用QTP 運行sumple查詢,但有多個語句像例如在細胞(X,6)下面的查詢存在的細胞: 「 使用LDatabase Exec的sp_DropObjectIfExists‘#tempTable’; 選擇COL1成#tempTable從maintable; 更新#tempTable設置COLV = 5 SELECT COUNT(1)如從TOTALCOUNT #tempTable 「

上面只是一個例如而不是確切的SQL查詢。 這整個集合在單個Excel表單元中。 我希望這是使用Qtp執行的。

目前,我在做什麼在QTP是:

Set objconnection = CreateObject("ADODB.Connection") 
objconnection.open"provider=blah blah blah" 
Set objrecordset= CreateObject("ADODB.Recordset") 
ws.cells(x,6).select ''''the above sql queries set is in this cell 
Sqlquery1= ws.cells(x,6).value 
objrecordset.Open Sqlquery1. objconnection 
Value1=objrecordset.Fields.Item(0) 

最後一行上面我得到錯誤說 「的項目無法在集合中找到對應requestef的名稱或序號」

我假設這是因爲在單個單元中有多個要執行的語句,但只有第一行是「正在使用LDatabase」正在執行。而不是所有的細胞內容。

你能幫我一下子完成整個事情嗎?

謝謝!

+0

'Sqlquery1。 objconnection'應該讀爲'Sqlquery1,objconnection'。每個單元格中的查詢是否總是返回一個表格?如果是這樣,它總是一張桌子嗎? –

+0

是的。在單元格中設置的每個查詢都會返回一個表。 – Neha

+0

[Microsoft推薦](https://support.microsoft。com/en-us/help/235340/prb-error-messaging-referencing-temp-table-with-ado-sqloledb)您將SET NOCOUNT ON;'添加到包含臨時表的查詢中。這也可以讓你包含聲明語句(例如:'DECLARE @MyVar INT = 5;')。 –

回答

0

SET NOCOUNT ON;前綴查詢。這將允許您在SQL語句中使用temp tables和變量。

下面的代碼演示了這一點。我已使用early binding使代碼更易於閱讀(工具 >>參考文獻 >>Microsoft ActiveX Data Objects 2.8 Library)。這些線之間

切換到測試:

  • rs.Open QueryA, cn, adOpenForwardOnly, adLockReadOnly
  • rs.Open QueryB, cn, adOpenForwardOnly, adLockReadOnly

QueryA將失敗。 QueryB將返回Jack

' Demo of using SET NOCOUNT ON;. 
' This option enabled the use of SQL vars and temp tables. 
Sub Test() 
    Dim cn As ADODB.Connection 
    Dim rs As ADODB.Recordset 

    Set cn = New ADODB.Connection 
    Set rs = New ADODB.Recordset 

    cn.Open "Driver={SQL Server};Server=YOUR-SEVER-NAME-HERE;Database=master;Trusted_Connection=Yes;" 

    ' QueryA fails, while QueryB does not. 
    ' Switch which line is commented out to test. 
    rs.Open QueryA, cn, adOpenForwardOnly, adLockReadOnly 
    'rs.Open QueryB, cn, adOpenForwardOnly, adLockReadOnly 

    ' This line will raise an error with QueryA. 
    ' This line will work with QueryB. 
    MsgBox rs.Fields(1).Value 

    rs.Close 
    cn.Close 
End Sub 

' Returns a sample query without NOCOUNT. 
Public Function QueryA() As String 

    QueryA = "    CREATE TABLE #ExampleA    " 
    QueryA = QueryA & "  (         " 
    QueryA = QueryA & "   Id  INT PRIMARY KEY,  " 
    QueryA = QueryA & "   Name VARCHAR(50) NOT NULL " 
    QueryA = QueryA & " );         " 
    QueryA = QueryA & "" 
    QueryA = QueryA & "  INSERT INTO #ExampleA (Id, Name) " 
    QueryA = QueryA & "  VALUES        " 
    QueryA = QueryA & "   (1, 'Jack'),     " 
    QueryA = QueryA & "   (2, 'Jill')      " 
    QueryA = QueryA & "  ;         " 
    QueryA = QueryA & "" 
    QueryA = QueryA & "  SELECT * FROM #ExampleA    " 
End Function 

' Returns a sample query with NOCOUNT. 
Public Function QueryB() As String 

    QueryB = "    SET NOCOUNT ON;      " 
    QueryB = QueryB & "" 
    QueryB = QueryB & "  CREATE TABLE #ExampleA    " 
    QueryB = QueryB & "  (         " 
    QueryB = QueryB & "   Id  INT PRIMARY KEY,  " 
    QueryB = QueryB & "   Name VARCHAR(50) NOT NULL " 
    QueryB = QueryB & " );         " 
    QueryB = QueryB & "" 
    QueryB = QueryB & "  INSERT INTO #ExampleA (Id, Name) " 
    QueryB = QueryB & "  VALUES        " 
    QueryB = QueryB & "   (1, 'Jack'),     " 
    QueryB = QueryB & "   (2, 'Jill')      " 
    QueryB = QueryB & "  ;         " 
    QueryB = QueryB & "" 
    QueryB = QueryB & "  SELECT * FROM #ExampleA    " 
End Function 

我在幾個醜陋的函數中嵌入了我的查詢的兩個版本。他們很難閱讀,但很容易分享。以下是工作查詢的乾淨版本。刪除非工作變體的第一行。

SET NOCOUNT ON; 

CREATE TABLE #ExampleA    
(         
    Id  INT PRIMARY KEY,   
    Name VARCHAR(50) NOT NULL  
);         

INSERT INTO #ExampleA (Id, Name)  
VALUES        
    (1, 'Jack'),      
    (2, 'Jill')      
;         

SELECT * FROM #ExampleA;    
相關問題