Conditional sql server input

Consider this main insert

insert into TableName (Col1,Col2,Col3) values (Val1,Val2,Val3)

I want this insertion to be done only if Val1! = Null and Val3! = Null How to do this?

+3
source share
2 answers

Is this what you are looking for?

IF (Val1 is not null AND Val3 is not null)
BEGIN 
    insert into TableName (Col1,Col2,Col3) values (Val1,Val2,Val3)
END 

At a second glance, BeachBlocker's answer is also very nice. I changed it a bit:

insert into TableName (Col1,Col2,Col3) select Val1,Val2,Val3 where Val1 is not null and Val3 is not null
+3
source
insert into TableName (Col1,Col2,Col3) select Val1,Val2,Val3 where Val1 is not null and Val3 is not null
+2
source

Source: https://habr.com/ru/post/1754563/


All Articles