DAO RecordsAffected 在 passthru 合并后没有 return 正确的值

DAO RecordsAffected does not return the right value after passthru merge

以下代码调用直通查询 (PTQ) 并应显示一条消息,指示受影响的记录数:

With CurrentDb
    .Execute "ArticleClientTmpUpdate"
    MsgBox .RecordsAffected & " records updated"
End With

PTQ 做了一个简单的合并:

merge into dbo.ArticlesClients ac
using (select * from dbo.ArticlesClientsTmp) act on ac.IdArticle = act.IdArticle and ac.IdClient = act.IdClient 
when matched then 
    update  SET ac.IdArticle = act.[idArticle], 
                ac.IdClient = act.[idclient], 
                ac.RefClient = act.[refClient], 
                ac.Commentaire = act.[commentaire], 
                ac.ModDate = getdate()
when not matched then 
    insert (idArticle, idClient, RefCLient, Commentaire)
    values (act.idArticle, act.idClient, act.RefCLient, act.Commentaire);

清空目标后从 SSMS 执行时 table,我可以看到 9200 行受到影响(例如创建)。
当使用上面的 VBA 执行相同的合并时,它说“导入了 0 条记录”,但它完成了工作并创建了 9200 条记录。
任何解释?这个问题有解决方法吗?使用 ADO connection 而不是 DAO CurrentDb 会更好吗?

这是设计使然。 ReturnsRecords = False 的传递查询被发送到服务器,从 Access 的角度来看就是这样。

.RecordsAffected 从未设置为传递查询。

您必须创建一个存储过程来执行您的 SQL,然后 returns 记录数。

来自https://bytes.com/topic/access/answers/204949-passthrough-queries-recordsaffected

CREATE PROCEDURE ...

SET NOCOUNT ON;

<your Update query here>

SELECT @@ROWCOUNT AS myROWCOUNT;