Using CASE or IF with INSERT

  • Hello,

    Within an INSERT statement where the VALUES are expressed, is it possible to evaluate one or more of the values with CASE or IF statements?

    Consider the following code;

    INSERT INTO snbat011_app_clnt

         (entr_cd,

          app_id,

          clnt_id,

          clnt_type,

          clnt_frst_name,

          clnt_mid_name,

          clnt_lst_name,

          clnt_gender)

    VALUES 

     (@EntrCode, @AppId, @ClientId, @FirstName, @MidName, @LastName,

    If @FormType = 'SimplifiedApp Or (@FormType = 'AccidentApp' And @FormId = 'AccidentAppRls0304') Then

            @Gender 

    Else

             '' )

    The entry of a value for clnt_gender depends on the statements in the If condition. If one condition is met, then the parameter value in @Gender is used, otherwise, the entry is an empty string.

    How would you recommend that I handle this kind of situation?

    Thank you for your help!

    CSDunn

  • I don't believe you can embed a CASE statement within an INSERT.  I've never gotten it to work.

    However, one possibility would be to create a UDF that takes the parameter and returns what you want inserted.

     

  • How about Evaluate the @FormType and @FormId parameters, to set the value of Gender ID then insert.

    You cannot do it inline in the Values Statement.

    How about.

    If @FormType = 'SimplifiedApp' Or

      (@FormType = 'AccidentApp' And @FormId = 'AccidentAppRls0304')

    Set @Gender = @Gender

    else

    set @Gender = ''

    INSERT INTO snbat011_app_clnt

         (entr_cd,

          app_id,

          clnt_id,

          clnt_type,

          clnt_frst_name,

          clnt_mid_name,

          clnt_lst_name,

          clnt_gender)

    VALUES

     (@EntrCode, @AppId, @ClientId, @FirstName, @MidName, @LastName, @Gender)

  • How about:

     

    INSERT INTO snbat011_app_clnt

         (entr_cd,

          app_id,

          clnt_id,

          clnt_type,

          clnt_frst_name,

          clnt_mid_name,

          clnt_lst_name,

          clnt_gender)

    SELECT

      @EntrCode, @AppId, @ClientId, @FirstName, @MidName, @LastName

     , CASE WHEN @FormType = 'SimplifiedApp' Or (@FormType = 'AccidentApp' And @FormId = 'AccidentAppRls0304') Then

            @Gender ELSE   '' END as Gender

     


    * Noel

  • As promised I have to agree with Noeld's solution .

  • Thanks for the help!

    CSDunn

  • I knew it!  


    * Noel

  • A promess is a promess .

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply