SQL:INSERT INTO中的条件(IF-ELSE)

时间:2013-01-08 12:39:36

标签: sql-server insert request

有可能吗?

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

我想测试一个值,如果变量$ company是空的,我想不写公司。

2 个答案:

答案 0 :(得分:25)

试试这个:

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*/

答案 1 :(得分:6)

可能不是,但是

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

应该是一个观众