2013-01-08 4 views
9

Ist es möglich?SQL: eine Bedingung (IF-ELSE) in INSERT INTO

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

Ich würde ein Wert wie Test ein, wenn die Variable $ Firma leer ist, würde ich gerne keine Firma schreiben.

+2

sein Haben Sie tun das Kontroll versucht, vor dem Einsatz zu tun? – Kane

+3

Welche DBMS benutzen Sie? –

+1

in meinem Fall kann ich es vorher nicht tun. Ich benutze SQL Server –

Antwort

20

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

Wahrscheinlich nicht, aber

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

Sollte ein goer