开发者

Conditional insert sql server

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜