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

2022-01-05 00:00:00 request insert sql-server

有可能吗?

<块引用>

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

我想测试一个值,如果变量 $company 为空,我不想写公司.

解决方案

试试这个:

INSERT INTO tblPeople (id, group, company)选择 1, 'myGroup',案件当@company 为空或@company = '' 然后'没有公司'否则'我的公司'作为公司结束/*从选项卡--<--可选*/

Is it possible ?

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

I would like test a value, an if the variable $company is empty, I would like write no company.

解决方案

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

相关文章