Monday, August 16, 2010

Insert record where not exist

If we want to insert record but do not want to make duplicates there are a number of ways to do it. The following scenerio is that user want to insert a record from a form and do not want to insert duplicates base on case id.

Insert into tbl_case (caseid, createdatetime) select '123456', '4/3/2010 12:00:00' from tbl_case where not exists (select caseid from tbl_case where caseid = '123456')

The trick part is the first select. It is actually not getting anything from the database. It won't work if we do not refer to the table this way.

The second select is just standard syntax using the "where not exists" clause.

No comments:

Post a Comment