I've got a stored procedure that will update a flag based on a county code, a URL or a combination of both. For instance, County 03 might have these two records:
county_code = 03
webservice_type_id = 1
webservice_URL = http://192.168.100.3/A
And
county_code = 03
webservice_type_id = 2
webservice_URL = http://192.168.100.3/B
And if I want to use my stored procedure to enable/disable the flag, I should be able to run:
execute dbo.usp_webservice_change_status
@enable = 1
@county_code = '03'
@webserviceURL = http://192.168.100.3/B
and update only the record that matches both condidtions. But what I am seeing is that the stored procedure updates the records for the county but throws the RAISERROR statement in the stored procedure.
For example, here are my current records:
And when I run the stored procedure:
I am getting the following:
But when I look at the records again, I see that they have been updated:
Here is the code for the stored procedure. Could anyone help me understand where I am wrong?
ALTER PROCEDURE [dbo].[usp_webservice_change_status]
@enable AS BIT,
@county_code AS CHAR(2) = NULL,
@webserviceURL AS VARCHAR(4000) = NULL
AS
BEGIN
SET NOCOUNT ON;
IF @enable IS NULL
RAISERROR ('The value for @enable should not be null', 15, 1);
IF ( @county_code IS NULL AND @webserviceURL IS NULL )
RAISERROR ('The value for @county_code and @webserviceURL cannot both be null', 15, 1);
-- Update for County Code Only
IF (@county_code IS NOT NULL AND @webserviceURL IS NULL )
UPDATE dbo.webservice_config
SET [enable] = @enable,
comments = CASE
WHEN @enable = 1
THEN 'Enabled by ' + SUSER_SNAME()
ELSE 'Disabled by ' + SUSER_SNAME()
END
WHERE county_code = @county_code;
-- Update for Webservice URL only
ELSE IF ( @county_code IS NULL AND @webserviceURL IS NOT NULL )
UPDATE dbo.webservice_config
SET [enable] = @enable,
comments = CASE
WHEN @enable = 1
THEN 'Enabled by ' + SUSER_SNAME()
ELSE 'Disabled by ' + SUSER_SNAME()
END
WHERE webservice_URL = @webserviceURL;
-- Update for both County Code and Webservice URL - but only if the records match
ELSE IF (@county_code IS NOT NULL AND @webserviceURL IS NOT NULL )
IF (@@ROWCOUNT) < 1
RAISERROR('Nothing Updated Due to Non Matching Records', 15, 1);
UPDATE dbo.webservice_config
SET [enable] = @enable,
comments = CASE
WHEN @enable = 1
THEN 'Enabled by ' + SUSER_SNAME()
ELSE 'Disabled by ' + SUSER_SNAME()
END
WHERE (county_code = @county_code
AND webservice_URL = @webserviceURL);
END;
The code I used to generate the correct response:
ALTER PROCEDURE [dbo].[usp_webservice_change_status]
@enable AS BIT,
@county_code AS CHAR(2) = NULL,
@webserviceURL AS VARCHAR(4000) = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Count INT = ( SELECT COUNT(1) FROM dbo.webservice_config WHERE county_code = @county_code AND webservice_URL = @webserviceURL );
....
ELSE IF ( @county_code IS NOT NULL AND @webserviceURL IS NOT NULL )
IF @Count > 0
UPDATE dbo.webservice_config
SET [enable] = @enable,
comments = CASE
WHEN @enable = 1 THEN 'Enabled by ' + SUSER_SNAME()
ELSE 'Disabled by ' + SUSER_SNAME()
END
WHERE ( county_code = @county_code AND webservice_URL = @webserviceURL );
ELSE
RAISERROR ('Nothing Updated Due to Non Matching Records', 15, 1 );