Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 662043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:20:04+00:00 2026-05-13T23:20:04+00:00

I am writing a stored procedure where i m using try catch block. Now

  • 0

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

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:20:05+00:00Added an answer on May 13, 2026 at 11:20 pm

    You can’t raise system errors. RAISERROR(2627…) is illegal. Beside the fact that you’re lying (no error 2627 occurred), you’re missing the inserts into the message format.

    Application should never rely on IDENTITY continuity, the fact that you complain that it ‘increases the autoincrement’ revels your application has a bug (in design for sure, if not in code). IDENTITY values may contain gaps, is part of their specifications.

    As to what is better, to insert and catch, or to try to update: it depends on your prevalent pattern. If the value is likely to exist, the UPDATE first strategy is better. If the value is likely not to exist or if the probability is unknown it is better to INSERT and catch the error, for reasons of performance and, more importantly, correctness (SELECT check is never correct under concurrency).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a stored procedure where I have an input parameter called my_size
I'm writing a stored procedure that needs to have a lot of conditioning in
I am a rookie in writing SQL stored procedure using C#. Just wandering is
I am writing a stored procedure and within this procedure I am using isNULL.
I am writing a stored procedure to update a transaction table. I will be
I have a stored procedure which selects a single field from a single row
I am messing up somewhere when writing a function to execute stored procedure. I
I would like to have a stored procedure that will update values in a
I have an insert statement that was deadlocking using linq. So I placed it
Writing a stored procedure in MS SQL Server 2008 R2, I want to avoid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.