If else in stored procedure sql server

后端 未结 8 1769
孤街浪徒
孤街浪徒 2021-02-02 07:19

I have created a stored procedure as follow:

Create Procedure sp_ADD_USER_EXTRANET_CLIENT_INDEX_PHY
(
@ParLngId int output
)
as
Begin
    SET @ParLngId = (Select         


        
8条回答
  •  Happy的楠姐
    2021-02-02 07:22

    You can try below Procedure Sql:

    Create Procedure sp_ADD_USER_EXTRANET_CLIENT_INDEX_PHY
    (
    @ParLngId int output
    )
    as
    Begin
        -- Min will return only 1 value, if 'Extranet Client' is found
        -- IsNull will take care of 'Extranet Client' not found, returning 0 instead of Null
        -- But T_Param must be a Master Table with ParStrNom having a Unique Index, if so Min is not reqd at all
        -- But 'PHY', 'Extranet Client' suggests that Unique Key has 2 columns, not just ParStrNom
    
        SET @ParLngId = IsNull((Select Min (ParLngId) from T_Param where ParStrNom = 'Extranet Client'), 0);
    
        -- Nothing changed below
    
        if  (@ParLngId = 0)
            Begin
                Insert Into T_Param values ('PHY', 'Extranet Client', Null, Null, 'T', 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL)
                SET @ParLngId = @@IDENTITY
        End
    
        Return  @ParLngId
        End
    

提交回复
热议问题