如何使用单个对象

时间:2017-08-29 11:01:52

标签: sql-server vba qtp

我有一个带有名为sqlQueries的列的Excel工作表。 每个单元格都有一组要运行的查询。

我可以使用qtp运行简单的查询 但是存在多个语句的单元格,例如单元格(x,6)中的以下查询: “     使用LDatabase     Exec sp_DropObjectIfExists'#tempTable';     从maintainble中选择col1到#tempTable;     更新#tempTable set colv = 5     从#tempTable中选择count(1)作为totalCount “

以上只是例如而不是确切的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 name或ordinal对应的集合中找到项目”

我假设这是因为单个单元格中有多个语句要被执行,但只有第一行“使用LDatabase”正在执行。而不是所有的细胞内容。

请你帮我一次完成整个事情。

谢谢!

1 个答案:

答案 0 :(得分:0)

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

以下代码证明了这一点。我已使用early binding使代码更易于阅读(工具>> 参考>> Microsoft ActiveX数据对象2.8图书馆)。

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

  • 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;