代码之家  ›  专栏  ›  技术社区  ›  John Lemp Chris Heald

是否使用ExecuteOnQuery重写SQL Server中受影响的行?

  •  2
  • John Lemp Chris Heald  · 技术社区  · 15 年前

    我有一个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
    
    2 回复  |  直到 15 年前
        1
  •  5
  •   Phil Sandler    15 年前

    不知道这是否有效,但您是否尝试过将nocount设置为on(然后在最终查询之前将nocount设置为off)?

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

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

        2
  •  0
  •   marc_s    15 年前

    不,在ADO.NET或SQL Server中无法重写或更改该行为。

    唯一的选择是捕获感兴趣的行数,并将其放入变量中并返回。