SQL : a condition (IF-ELSE) in INSERT INTO

前端 未结 2 1139
无人及你
无人及你 2021-01-08 00:38

Is it possible ?

INSERT INTO tblPeople (id, group, company) VALUES (1, \'myGroup\', 
IF($company = \'\') BEGIN \'no company\' ELSE \'myCompany\         


        
相关标签:
2条回答
  • 2021-01-08 01:02

    Try this:

    INSERT INTO tblPeople (id, group, company) 
    select 1, 'myGroup', 
    case 
      when @company is null or  @company = '' then 'no company' 
      else 'myCompany' 
    END as  company
    /*from tab --<-- optional*/
    
    0 讨论(0)
  • 2021-01-08 01:22

    Probably not, but

    INSERT INTO tblPeople (id, [group], company) 
    Select 1, 'myGroup', Case When @Company = '' then 'no company' Else 'my company' End
    

    Should be a goer

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