2016-11-10 80 views
0

我有一個存儲過程,將更新基於一個縣代碼,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語句。

例如,這裏是我目前的記錄:

enter image description here

當我運行存儲過程:

enter image description here

我得到以下幾點:

enter image description here

但是,當我在記錄再看看,我看到他們已更新: enter image description here

這裏是存儲過程的代碼。任何人都可以幫助我瞭解我的錯在哪裏?

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; 
+0

好,當然,你先加錯誤,然後運行'UPDATE' – Lamak

+0

行 - 我想提高錯誤將阻止從運行中更新。 – MISNole

+0

所以,你其實是想不更新表,除非參數之一是'null'? – Lamak

回答

0

我用來生成正確的響應代碼:

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); 
1

在你最後的ELSE IF你在哪裏檢查未參數不爲空,下一行使用

IF (@@ROWCOUNT) < 1 

但我不明白,你正在運行一個查詢,看是否提供的參數有匹配的記錄。

我想你會想運行像

IF(SELECT count(1) FROM dbo.webservice_config) <> 1 
    RAISEERROR 
ELSE 
    Do your update 

只是一個想法。

+0

感謝保羅 - 我將其標記爲正確的,因爲根據關你的答案,我能夠改寫SQL使PROC工作按預期。 – MISNole