简体   繁体   中英

determine record exist by exception of sql is Principles?

I have a table with primary key and I want to insert new record to that . In my code , I check if record exists by exception. In sql store procedure I have insert code and surround by exception , in my application I execute that sp and want to insert new record , if my record exist in table , insert code got error

"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Sup_Item_Sup_Item_Cat". The conflict occurred in database test, table test. The statement has been terminated."

and go to catch statement, In my application I check the error that was returned by sql, and show a message box to user that record is exist .

I want to know, is this way is Principles? or I must use if exist statement in sql ?

Exception should never be used when you can avoid it and return a value. Exception is a "stress" on a system and much slower than any other way.

Its customary for a SP to return 0 if everything is Ok and a negative value if there is an error. Either check your SP return code in application or use out parameter to determine problem. An error should be truly an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM