I am writing a stored procedure where i m using try catch block.
Now i have a unique column in a table. When i try to insert duplicate value it throws exception with exception no 2627.
I want this to be done like this
if (exists(select * from tblABC where col1='value')=true) raiseError(2627)--raise system error that would have thrown if i would have used insert query to insert duplicate value
EDIT:
I m using transaction which gets rollback on handling exception hence leading to increment of @@Identity value that got executed in previous queries b4 the exception occurred.
I want to check all such exception that could occur b4 actually inserting. For doing this, i would be checking for exception that could raise error manually using select statement with if else
. Here i will be able to catch unique key violation but exception will not be occurred so i will be throwing exception deliberately here but that exception i want should be a system exception i.e error 2627
And which method will be better, using insert query or checking for duplicate value before insertion using Select query ?
IS IT POSSIBLE ANYHOW TO RAISE SYSTEM EXCEPTION ON CATCHING EXCEPTION MANUALLY i.e THROWING SAME EXCEPTION THAT SQL WOULD HAVE THROWN