如何 运行 使用单个对象一次性在 qtp 中进行多个查询

How to run multiple queries in qtp in one shot using single object

我有一个 excel sheet,其中有一列名为 sqlQueries。 每个单元格都有一组查询 运行.

我能够 运行 使用 qtp 进行查询 但是具有多个语句的单元格,例如在单元格(x,6)中存在以下查询: “ 使用大数据库 执行 sp_DropObjectIfExists '#tempTable'; Select col1 从 maintable 进入#tempTable; 更新#tempTable set colv=5 Select count(1) 作为来自#tempTable 的 totalCount “

以上只是一个例子而不是确切的 sql 查询。 整个集合在一个 excel sheet 的单元格中。 我希望使用 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)

对于上面的最后一行,我收到错误提示 "Items cannot be found in the collection corresponding to requestef name or ordinal"

我假设这是因为单个单元格中有多个要执行的语句,但只有第一行 "use LDatabase" 正在执行。而不是所有的单元格内容。

你能帮我一次完成整个事情吗?

谢谢!

在您的查询前加上 SET NOCOUNT ON;。这将允许您在 SQL 语句中使用 temp tables 和变量。

下面的代码演示了这一点。我使用 early binding 使代码更易于阅读 (Tools >> References >> Microsoft ActiveX数据对象 2.8 库)。

在这些行之间切换以进行测试:

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

QueryA 将失败。 QueryB 将 return 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;