How can I get the number of records affected by a stored procedure?

前端 未结 6 1994
轮回少年
轮回少年 2020-12-04 20:32

For INSERT, UPDATE and DELETE SQL statements executed directly against the database, most database providers return the count of rows

6条回答
  •  清歌不尽
    2020-12-04 21:19

    @@RowCount will give you the number of records affected by a SQL Statement.

    The @@RowCount works only if you issue it immediately afterwards. So if you are trapping errors, you have to do it on the same line. If you split it up, you will miss out on whichever one you put second.

    SELECT @NumRowsChanged = @@ROWCOUNT, @ErrorCode = @@ERROR
    

    If you have multiple statements, you will have to capture the number of rows affected for each one and add them up.

    SELECT @NumRowsChanged = @NumRowsChanged  + @@ROWCOUNT, @ErrorCode = @@ERROR
    

提交回复
热议问题