我有一個存儲過程,將更新基於一個縣代碼,URL或兩者結合的標誌。例如,縣03可能有這兩個記錄:存儲過程投擲的錯誤,但更新記錄
county_code = 03
webservice_type_id = 1
webservice_URL = http://192.168.100.3/A
而且
county_code = 03
webservice_type_id = 2
webservice_URL = http://192.168.100.3/B
如果我想用我的存儲過程來啓用/禁用標誌,我應該能夠運行:
execute dbo.usp_webservice_change_status
@enable = 1
@county_code = '03'
@webserviceURL = http://192.168.100.3/B
並僅更新與兩個條件相匹配的記錄。但是我所看到的是存儲過程更新縣的記錄,但在存儲過程中拋出RAISERROR語句。
例如,這裏是我目前的記錄:
當我運行存儲過程:
我得到以下幾點:
這裏是存儲過程的代碼。任何人都可以幫助我瞭解我的錯在哪裏?
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;
好,當然,你先加錯誤,然後運行'UPDATE' – Lamak
行 - 我想提高錯誤將阻止從運行中更新。 – MISNole
所以,你其實是想不更新表,除非參數之一是'null'? – Lamak