【问题标题】:Using temporary table in an ADO Recordset在 ADO 记录集中使用临时表
【发布时间】:2015-01-22 20:11:32
【问题描述】:

请看下面的代码:

rsTitle.Open "CREATE TABLE #TestTable (testtitle varchar(10)) INSERT INTO #TestTable values ('TestTitle') select * from #testtable", objCon.ActiveCon, adOpenStatic, adLockReadOnly

rsTitle 总是在上面的行执行后关闭。为什么是这样?它应该返回一行。

【问题讨论】:

  • 尝试在语句开头添加set nocount on

标签: tsql vb6


【解决方案1】:

尝试使用SET NOCOUNT ON。将SET NOCOUNT ON放入ADO使用的SQL语句中,如下图:

Dim adoCn As adoDb.Connection
Dim adoCm As adoDb.Command
Dim rsTitle As adoDb.Recordset

Set adoCn = New adoDb.Connection
...    
Set adoCm = New adoDb.Command
With adoCm
    Set .ActiveConnection = adoCn
    .CommandType = adCmdText
    .CommandText = "CREATE TABLE #TestTable (testtitle varchar(10)) " & _
                   "INSERT INTO #TestTable(testtitle) values ('TestTitle') " & _
                   "SELECT * FROM #TestTable go"
    .Execute 
End With

Set rsTitle = New adoDb.Recordset
With rsTitle
    Set .ActiveConnection = adoCn
    .Open "SET NOCOUNT ON"
End With
rsTitle.Open adoCm, , , ,

【讨论】:

  • 感谢您的评论。这在问题中的代码之前起作用。但是,它并没有解决我遇到问题的实际代码的问题。
【解决方案2】:

对于那种类型的混合 DDL+SELECT 语句,你最好:

Set rstitle = objCon.ActiveCon.Execute("SET NOCOUNT ON; CREATE TABLE #TestTable (testtitle varchar(10)); INSERT #TestTable values ('TestTitle'); select * from #testtable", adCmdText)

【讨论】:

    猜你喜欢
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多