我正在尝试使用ADO对象将传递给SQL服务器的sql命令的结果返回到Excel,但我的Recordset一直保持关闭/清空状态。我的猜测是我的sql语句没有返回任何内容 - 尽管我认为它应该返回一条记录。
我正在运行的SQL是一个530行的野兽,但它归结为一个MERGE
语句,该语句应该将结果转储到表var中,然后从中进行选择。这个select语句是我想要返回到ADO记录集的。
这是我正在使用的通用SQL语句:
MERGE ldw_plan_working AS target
USING source
ON target.DT_Code = source.DT_Code
AND target.Country = source.Country
AND target.Channel = source.Channel
AND target.HierarchyID = source.HierarchyID
AND target.lifecycle = source.lifecycle
WHEN MATCHED
THEN UPDATE SET
[Sales_TOT_Local] = source.[Sales_TOT_Local],
[Sales_TOT_USD] = source.[Sales_TOT_USD],
[Sales_TOT_CAD] = source.[Sales_TOT_CAD],
[Sales_CLR_Local] = source.[Sales_CLR_Local],
[Sales_CLR_USD] = source.[Sales_CLR_USD],
[Sales_CLR_CAD] = source.[Sales_CLR_CAD],
[PM_TOT_Local] = source.[PM_TOT_Local],
[PM_TOT_USD] = source.[PM_TOT_USD],
[PM_TOT_CAD] = source.[PM_TOT_CAD],
[PM_CLR_Local] = source.[PM_CLR_Local],
[PM_CLR_USD] = source.[PM_CLR_USD],
[PM_CLR_CAD] = source.[PM_CLR_CAD],
[Sales_TOT_U] = source.[Sales_TOT_U],
[Sales_CLR_U] = source.[Sales_CLR_U],
[Receipt_USD] = source.[Receipt_USD],
[Receipt_U] = source.[Receipt_U]
WHEN NOT MATCHED
THEN
INSERT([DT_Code],
[Country],
[Channel],
[HierarchyID],
[Lifecycle],
[Sales_TOT_Local],
[Sales_TOT_USD],
[Sales_TOT_CAD],
[Sales_CLR_Local],
[Sales_CLR_USD],
[Sales_CLR_CAD],
[PM_TOT_Local],
[PM_TOT_USD],
[PM_TOT_CAD],
[PM_CLR_Local],
[PM_CLR_USD],
[PM_CLR_CAD],
[Sales_TOT_U],
[Sales_CLR_U],
[Receipt_USD],
[Receipt_U])
VALUES
(source.[DT_Code],
source.[Country],
source.[Channel],
source.[HierarchyID],
source.[Lifecycle],
source.[Sales_TOT_Local],
source.[Sales_TOT_USD],
source.[Sales_TOT_CAD],
source.[Sales_CLR_Local],
source.[Sales_CLR_USD],
source.[Sales_CLR_CAD],
source.[PM_TOT_Local],
source.[PM_TOT_USD],
source.[PM_TOT_CAD],
source.[PM_CLR_Local],
source.[PM_CLR_USD],
source.[PM_CLR_CAD],
source.[Sales_TOT_U],
source.[Sales_CLR_U],
source.[Receipt_USD],
source.[Receipt_U]
)
OUTPUT $action
INTO @tableVar;
INSERT INTO @tableVar(MergeAction)
VALUES('nothing');
SELECT [UPDATE],
[INSERT],
[DELETE]
FROM @tableVar PIVOT(COUNT(MergeAction) FOR MergeAction IN([UPDATE],
[INSERT],
[DELETE],
[nothing])) AS piv;
vba函数看起来像这样:
Public Function Run_SQL_Cmd(sql As String)
Dim cnn As Object
Dim cmd As Object
Dim rs As Object
Dim recordsAffected As Integer
Set cnn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
Set rs = CreateObject("ADODB.Recordset")
cnn.Open strBEConnection
cnn.CommandTimeout = 0
With cmd
.ActiveConnection = cnn
.CommandText = sql
.CommandType = 1
Set rs = .Execute
End With
'... do stuff with the recordset
rs.Close
cnn.Close
Set cnn = Nothing
Set rs = Nothing
End Function
rs
总是返回0字段计数,关闭记录集,我无法做任何事情。但是当我在SQL服务器中直接运行相同的sql时,显然我得到了最终select语句的结果。谁知道我在这里做错了什么?
提前致谢
答案 0 :(得分:2)
好吧,我明白了。如果命令文本中有多个SQL语句,则需要在SQL语句中将其全部添加到“SET NOCOUNT ON;
”之前。这将使服务器返回最终select语句的结果。非常烦人的简单修复。