sql - Is it OK to swallow an exception for DB INSERT -


is ok swallow duplicate key violation exceptions inserts or should check if record exists?

so let's have table photo 1 field: photoname. i'm looking through file directory add items photo. in process, it's possible when find photoname, might in database. there 2 ways go this:

1) //look see if exists before adding it. add if not exist.  bool photoexists = sqlselectstatementtocheckifthephotoexists(photoname); if(!photoexists)   sqlcommandtoinsertphoto(photoname)  or 2) //assume doesn't exist. if does, catch , ignore. try {   sqlcommandtoinsertphoto(photoname); } catch(duplicatekeyexception ex) {   //swallow , continue on if nothing happened. } 

on 1 hand, don't notion of "swallowing" exception, on other hand, try...catch uses 1 call db. happens in sql server.

you should not "swallow" exception. should trying find these duplicates , not insert them if needed.

on method checking not exists on key.

insert targettable  select      keyid,     blah,     blerg, sourcetable s not exists (     select 1     targettable t     s.keyid = t.keyid     ) 

this method allow insert new rows table. method of course not account matching may need update, that's outside scope of question should still thought about. users use merge i'll post example of when time.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -