If else in stored procedure sql server

后端 未结 8 1651
孤街浪徒
孤街浪徒 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条回答
  • 2021-02-02 07:42

    Try this one -

    CREATE PROCEDURE sp_ADD_USER_EXTRANET_CLIENT_INDEX_PHY
    AS
    BEGIN
    
        DECLARE @ParLngId INT
        SELECT TOP 1 @ParLngId = ParLngId
        FROM dbo.T_Param
        WHERE ParStrNom = 'Extranet Client'
    
        IF (@ParLngId = 0)
        BEGIN
            INSERT INTO dbo.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)
    
            RETURN SCOPE_IDENTITY()
        END
        ELSE BEGIN
    
            RETURN @ParLngId 
    
        END
    
    END
    
    0 讨论(0)
  • 2021-02-02 07:46

    Thank you all for your answers but I figured out how to do it and the final procedure looks like that :

    Create Procedure sp_ADD_RESPONSABLE_EXTRANET_CLIENT
    (
    @ParLngId int output
    )
    as
    Begin
    if not exists (Select ParLngId from T_Param where ParStrIndex = 'RES' and ParStrP2 = 'Web')
        Begin
                INSERT INTO T_Param values('RES','¤ExtranetClient', 'ECli', 'Web', 1, 1, Null, Null, 'non', 'ExtranetClient', 'ExtranetClient', 25032, Null, 'informatique.interne@company.fr', 'Extranet-Client', Null, 27, Null, Null, Null, Null, Null, Null, Null, Null, 1, Null, Null, 0 )
                SET @ParLngId = @@IDENTITY
        End
    Else
        Begin
                SET @ParLngId = (Select top 1 ParLngId from T_Param where ParStrNom = 'Extranet Client')
                Return @ParLngId
        End   
    End
    

    So the thing that I found out and which made it works is:

    if not exists

    It allows us to use a boolean instead of Null or 0 or a number resulted of count()

    0 讨论(0)
提交回复
热议问题