使用存储过程更新列中的一些数据

时间:2016-11-30 10:28:53

标签: sql-server stored-procedures

我有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

1 个答案:

答案 0 :(得分: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