2016-11-30 51 views
0

我有person表和peSubmbitedFL列。我需要編寫一個存儲過程來將此列中的數據從0更新爲1,以獲取特定ID的列表。使用存儲過程更新列中的某些數據

我的代碼出錯了?

ALTER PROCEDURE [dbo].[pr_PersonAssignSubmitted] 
    @AppIDs varchar(8000), 
    @RetVal int OUTPUT 
AS 
    SET @RetVal = 0 

    SET XACT_ABORT ON 
BEGIN TRAN 
    DECLARE @AppID int 

    DECLARE CURSOR_COLUMNS CURSOR LOCAL FOR 
     SELECT item  
     FROM dbo.[fn_ParseStrList](@AppIDs, ',') --pretvora od string vo tabela 
     WHERE CAST(item as int) IN (SELECT pePersonID 
            FROM Person 
            WHERE pePersonID = @AppID) 

    OPEN CURSOR_COLUMNS 

    FETCH NEXT FROM CURSOR_COLUMNS INTO @AppID  -- item stavi go vo @AppID 

    WHILE @@FETCH_STATUS = 0 
    BEGIN  
     UPDATE Person 
     SET pePersonID = @AppID, peIsSubmittedFL = 1; 

     FETCH NEXT FROM CURSOR_COLUMNS INTO @AppID 
    END 

    IF @@ERROR = 0 SET @RetVal = 1 

    COMMIT TRAN 
    SET XACT_ABORT OFF 

    WHILE @@FETCH_STATUS = 0 
    BEGIN  
     UPDATE Person 
     SET pePersonID = @AppID, peIsSubmittedFL = 1; 

     FETCH NEXT FROM CURSOR_COLUMNS INTO @AppID 
    END 

    IF @@ERROR = 0 
     SET @RetVal = 1 

    COMMIT TRAN 
    SET XACT_ABORT OFF 
+0

我想你'UPDATE'的說法應該是'UPDATE人所設定的peIsSubmittedFL = 1 WHERE pePersonID = @ AppID' – Abhishek

+1

@Abhishek是的,這就是我想的太多,否則,OP在這裏試圖實現什麼是沒有意義的。 –

+0

@ M.Ali - 是的,反正OP沒有具體說明'代碼中的錯誤',你的答案會做這項工作 – Abhishek

回答

0

我不確定,但我想!你需要的是這樣的.....

CREATE PROCEDURE [dbo].[pr_PersonAssignSubmitted] 
    @AppIDs varchar(8000) 
, @RetVal int OUTPUT 
AS 
BEGIN 
SET NOCOUNT ON; 
SET XACT_ABORT ON; 
    BEGIN TRAN; 

     WITH PersonIDs AS 
     (
      SELECT item AS PersonID 
      FROM dbo.[fn_ParseStrList](@AppIDs, ',') 
     ) 
     UPDATE P 
      SET P.peIsSubmittedFL = 1 
     FROM PersonIDs i 
     INNER JOIN Person P ON P.pePersonID = i.PersonID 

     SET @RetVal = @@ROWCOUNT; 
    COMMIT TRAN; 
SET XACT_ABORT OFF; 
END 
+0

謝謝@ M.Ali,這很好。 – koda5