SQL Server affected rows @@ rowcount is always 0. issues

Stored procedure is as follows:

WHILE( 1=1 )
begin
	Begin Try
		Begin Transaction

		 Update RIS.dbo.A 
         set registerid_with_number = CDRDB_VIEW.DBO.ONLY_GET_Number(hisexamineid)
		 where  registerid IN (
                                  SELECT TOP 10000 registerid 
                                    FROM RIS.dbo.A 
                                   WHERE registerid_with_number IS NULL
                              )
        PRINT '更新一次'
		--全部更新完后,退出
		if @@rowcount = 0  
		begin
			COMMIT Transaction
			PRINT '最后提交一次'
			BREAK
		end


		COMMIT Transaction
		PRINT '提交一次'
		waitfor delay '00:00:01'  --睡眠1秒,避免锁表

	   End Try

	   Begin Catch
       .........................--省略

Run the process, each time first time out of the value of print @@ rowcount, and each time 0, update obviously affected the 10,000 line, how could the number of rows affected is zero it? At first I thought SQL SERVER is automatically submitted to refresh the affected rows, later changed to manual submission is still consistent @@ rowcount 0

finally:

      @@ rowcount represents not only the Insert, OK Update, Delete, Select returned rows other statements, it also recorded.

       I'm in the middle of print 'updated' to reset @@ rowcount, it has been 0

Solution:

      The @@ rowcount followed in the next line to be detected SQL statement, any statement can not be reset.

Update RIS.dbo.A  

set registerid_with_number = CDRDB_VIEW.DBO.ONLY_GET_Number(hisexamineid)
 where  registerid IN (

                                        SELECT TOP 10000 registerid

                                           FROM  RIS.dbo.A 

                                       The IS NULL registerid_with_number the WHERE)
 - total update after exit
IF = 0 @@ rowCount  
 the begin
           a COMMIT the Transaction
           --print 'last commit time'
             BREAK
the END

 

Published 35 original articles · won praise 61 · views 160 000 +

Guess you like

Origin blog.csdn.net/woailyoo0000/article/details/101782317