存储过程流程判断

存储过程如下:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SP_TableName_Save]
    (
      @Id BIGINT = 0 ,
      @Id_T1 BIGINT = 0 ,
      @IsOk NVARCHAR(10) ,
      @InfoState_T1 INT ,
      @MSG VARCHAR(255) = '' OUTPUT
    )
AS
    BEGIN
        SET NOCOUNT ON
        SET XACT_ABORT ON
        BEGIN TRAN       
          
        DECLARE @A VARCHAR(10)= ''
        DECLARE @B VARCHAR(10)= ''
        DECLARE @Id_T1Old BIGINT
        DECLARE @InfoState_T1Old INT
         
        SELECT  @Id_T1Old = Id_T1 ,
                @InfoState_T1Old = InfoState_T1
        FROM    [T2]
        WHERE   Id_T2 = @Id_T2
                
     
   
        IF ( @Id_T2 = 0
             AND @IsOk = '通过'
           ) --新增                   
            BEGIN
                SET @A = '1';
                SET @B = '1';
            END
        ELSE --修改
            BEGIN
                IF ( @IsOk = '通过' )
                    BEGIN
                        IF ( @Id_T1 <> @Id_T1Old )
                            BEGIN
                                SET @A = '3' + '1'
                                SET @B = '2'
                            END                 
                        ELSE
                            BEGIN
                                SET @A = '1'
                                SET @B = '2'
                            END
                    END
                ELSE --不通过
                    BEGIN
                        IF ( @Id_T1 <> @Id_T1Old )
                            BEGIN
                                SET @A = '3' + '4'
                                SET @B = '2'
                            END                 
                        ELSE
                            BEGIN
                                SET @A = '4'
                                SET @B = '2'
                            END
                    END                   
            END 
          
            
        IF @A LIKE '%1%'
            DELETE  dbo.[T1]
            WHERE   Id_T1 = @Id_T1
        IF @A LIKE '%2%'
            UPDATE  dbo.[T1]
            SET     InfoState_T1 = '标记删除'
            WHERE   Id_T1 = @Id_T1                            
        IF @A LIKE '%3%'
            UPDATE  dbo.[T1]
            SET     InfoState_T1 = @InfoState_T1Old
            WHERE   Id_T1 = @Id_T1Old            
        IF @A LIKE '%4%'
            UPDATE  dbo.[T1]
            SET     InfoState_T1 = @InfoState_T1
            WHERE   Id_T1 = @Id_T1
                            
            
        IF @B = '1'
            BEGIN                       
                        
                INSERT  INTO [dbo].[T2]
                        ( Id_T1, IsOK, InfoState_T1 )
                VALUES  ( @Id_T1, @IsOk, @InfoState_T1 )
                SET @Id_T2 = SCOPE_IDENTITY()
            END
        ELSE
            UPDATE  [dbo].[T2]
            SET     Id_T1 = @Id_T1 ,
                    IsOK = @IsOk ,
                    InfoState_T1 = @InfoState_T1
            WHERE   Id_T2 = @Id_T2
                    AND Id_T1 = @Id_T1                           
        COMMIT TRAN
        RETURN @@ERROR
    END
    

本次记录不是研究业务逻辑开发,而是个人觉得这种写法方便阅读,修改

posted on 2016-07-10 12:58  乘风寻觅  阅读(362)  评论(0编辑  收藏  举报

导航