开发者

Conditional insert sql server

开发者 https://www.devze.com 2023-01-06 21:52 出处:网络
Let\'s consider this basic insert insert into TableName (Col1,Col2,Col3) values (Val1,Val2,Val3) i want开发者_Go百科 this insert to be done only if Val1 !=null and Val3!=null

Let's consider this basic insert

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

i want开发者_Go百科 this insert to be done only if Val1 !=null and Val3!=null How to accomplish this?


Is this what you're looking for?

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

On second thought BeachBlocker's answer is quite nice too. I've modified it a little:

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


insert into TableName (Col1,Col2,Col3) select Val1,Val2,Val3 where Val1 is not null and Val3 is not null
0

精彩评论

暂无评论...
验证码 换一张
取 消