使用ExecuteNonQuery覆盖SQL Server中受影响的行?

duc*_*rth 2 .net sql-server ado.net sql-server-2008

我有一个insert语句,它将一些数据拉入一些表变量,然后根据该数据将一些数据插入到几个表中.我只关心插入真实表而不是表变量的行,但ExecuteNonQuery将返回所有@@ ROWCOUNT的总和.我想知道有没有办法覆盖使用ExecuteNonQuery返回的rowcount?

我知道我可以使用ExecuteScalar或输出变量作为替代.

这是一个将其简化为一个简单示例的示例:

CREATE TABLE VersionExample ( Version Varchar(255) )  

Declare @RowCountICareAbout int

DECLARE @Example TABLE ( Version Varchar(255) )  

INSERT INTO @Example Select @@VERSION

INSERT INTO VersionExample SELECT Version FROM @Example

SET @RowCountICareAbout = @@ROWCOUNT

--Use @RowCountICareAbout as the rows affected returned to ExecuteNonQuery
Run Code Online (Sandbox Code Playgroud)

Phi*_*ler 5

不知道这是否可行,但你是否尝试过SET NOCOUNT(然后在最终查询之前设置NOCOUNT OFF)?

更新:此博客文章和评论似乎表明这确实有效:

http://petesbloggerama.blogspot.com/2006/10/note-to-self-set-nocount-on-not.html