Solved

Simple SQL Question

Posted on 2008-06-18
4
346 Views
Last Modified: 2013-11-06
HI Experts

Please look at the attached code, it is an auto-generated code by MS Enterprise Library, Can you explain what is the purpose of the check for @@rowcount value and why if it is 0 , we have any concurrency problem. For me , it seems that the TRY/CATCH is enough to handle all errors and if the update have no rows to work on , @@rowcount will be 0 and it is completely OK.
Am I missing something? or I can safely remove this check and stay only with the TRY/CATCH ???

Thanks


ALTER PROCEDURE [dbo].[UpdateVmStatistics]

	@cpu_sys decimal(18,0) = NULL,

	@cpu_user decimal(18,0) = NULL,

	@elapsed_time decimal(18,0) = NULL,

	@rx_dropped_1 decimal(18,0) = NULL,

	@rx_dropped_2 decimal(18,0) = NULL,

	@rx_rate_1 decimal(18,0) = NULL,

	@rx_rate_2 decimal(18,0) = NULL,

	@tx_dropped_1 decimal(18,0) = NULL,

	@tx_dropped_2 decimal(18,0) = NULL,

	@tx_rate_1 decimal(18,0) = NULL,

	@tx_rate_2 decimal(18,0) = NULL,

	@usage_cpu_percent int = NULL,

	@usage_mem_percent int = NULL,

	@usage_network_percent int = NULL,

	@vm_guid uniqueidentifier,

	@vm_if_id_1 int = NULL,

	@vm_if_id_2 int = NULL,

	@vm_if_name_1 nvarchar(40) = NULL,

	@vm_if_name_2 nvarchar(40) = NULL,

	@vm_line_rate_1 int = NULL,

	@vm_line_rate_2 int = NULL

AS

BEGIN

	SET NOCOUNT ON
 

	BEGIN TRY

	UPDATE [dbo].[vm_statistics] 

	SET [cpu_sys] = @cpu_sys, [cpu_user] = @cpu_user, [elapsed_time] = @elapsed_time, [rx_dropped_1] = @rx_dropped_1, [rx_dropped_2] = @rx_dropped_2, [rx_rate_1] = @rx_rate_1, [rx_rate_2] = @rx_rate_2, [tx_dropped_1] = @tx_dropped_1, [tx_dropped_2] = @tx_dropped_2, [tx_rate_1] = @tx_rate_1, [tx_rate_2] = @tx_rate_2, [usage_cpu_percent] = @usage_cpu_percent, [usage_mem_percent] = @usage_mem_percent, [usage_network_percent] = @usage_network_percent, [vm_if_id_1] = @vm_if_id_1, [vm_if_id_2] = @vm_if_id_2, [vm_if_name_1] = @vm_if_name_1, [vm_if_name_2] = @vm_if_name_2, [vm_line_rate_1] = @vm_line_rate_1, [vm_line_rate_2] = @vm_line_rate_2

	WHERE [vm_guid]=@vm_guid

        /* Can you explain this ??? */

	IF @@ROWCOUNT = 0

	BEGIN

		RAISERROR('Concurrent update error. Updated aborted.', 16, 2)

	END

    END TRY
 

    BEGIN CATCH

		EXEC RethrowError;

	END CATCH	
 

	SET NOCOUNT OFF

END

Open in new window

0
Comment
Question by:elimesika
4 Comments
 
LVL 13

Accepted Solution

by:
Wizilling earned 167 total points
Comment Utility
Basically , if you update something and you get 0 rows updates it is not considered an error. so even if u wuold just have used a try / catch block, a update of 0 records would mean that 0 rows updated successfully. .This is not an error... The reason to have a if rowcount =0 means since 0 rows was update, treat this as a error and raise a custom error ...

0
 
LVL 2

Assisted Solution

by:TheLastStraw
TheLastStraw earned 166 total points
Comment Utility
The @@ROWCOUNT will return 0 if no rows are affected by preceding update based on filter conditions but does not raise an exception.  In the procedure the logic appears to be to raise an exception if 0 rows are updated.

The Try/Catch will trap errors if you had something like data conversion error based on parameters passed.

I would leave as is.
0
 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 167 total points
Comment Utility
The TRY...CATCH block is there to guard against an execution error.  Returning 0 records from the Update is not an execution error, but probably indicates some other kind of significant error in your database - you attempted to update at least one record, which then did not update for some reason - possibly the WHERE clause did not successfully identify an existing record in the table (which your code apparently expected to find).

I also would leave the Stored Procedure precisely as it is.  Eliminating the test on @@ROWCOUNT could end up masking problems in your database, which are not sufficient to cause a run-time error, but are symptomatic of significant data-related problems that you may need to investigate.

AW
0
 
LVL 19

Author Closing Comment

by:elimesika
Comment Utility
Thanks for your answers, I have splited the points between all 3 answers I got.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now