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 9309693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:55:49+00:00 2026-06-19T00:55:49+00:00

Given this table CREATE TABLE [dbo].[Table_2]( [X] [int] IDENTITY(1,1) NOT NULL, [Y] [varchar](30) NULL,

  • 0

Given this table

CREATE TABLE [dbo].[Table_2](
    [X] [int] IDENTITY(1,1) NOT NULL,
    [Y] [varchar](30) NULL,
 CONSTRAINT [PK_Table_2] PRIMARY KEY CLUSTERED 
(
    [X] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

When I run the following code against the empt table, the first insert succeeds but the second one will fail because the code is attempting to insert a duplicate value for an identity column.

BEGIN

    SET IDENTITY_INSERT Table_2 ON

    PRINT 'Insert 1'

    INSERT INTO Table_2
                (X, Y)
    VALUES      (3, CAST(getdate() AS varchar(30)) )

    PRINT 'Insert 2'

    INSERT INTO Table_2
                (X, Y)
    VALUES      (3, CAST(getdate() AS varchar(30)) )

    PRINT 'Insert 3'

    INSERT INTO Table_2
                (X, Y)
    VALUES      (4, CAST(getdate() AS varchar(30)) )

    SET IDENTITY_INSERT Table_2 OFF    

END 

output:

Insert 1

(1 row(s) affected)
Insert 2

Msg 2627, Level 14, State 1, Line 13
Violation of PRIMARY KEY constraint ‘PK_Table_2’. Cannot insert duplicate key in object ‘dbo.Table_2’.

Insert 3

(1 row(s) affected)

Instead of the script topping execution at the point where the error is encountered, execution continues with the next insert statement, which succeeds.

I realize that I can use a TRY CATCH block to catch the error when it occurs but my question is this:

I notice that the severity of the error is 14. I assume that an error of sufficient severity level would cause the execution to abort. Can you tell me if this is corerct and what min severity level is needed to cause teh script to abort?

  • 1 1 Answer
  • 0 Views
  • 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-06-19T00:55:51+00:00Added an answer on June 19, 2026 at 12:55 am

    You can use SET options to stop execution of a transaction on an error:

    SET XACT_ABORT ON
    

    http://msdn.microsoft.com/en-us/library/ms188792.aspx

    However, you’ll need to wrap your code in a transaction to get the all-or-nothing behavior.

    SET XACT_ABORT ON
    BEGIN  TRAN
    
    SET IDENTITY_INSERT Table_2 ON
    
    PRINT 'Insert 1'
    
    INSERT INTO Table_2
                (X, Y)
    VALUES      (3, CAST(getdate() AS varchar(30)) )
    
    PRINT 'Insert 2'
    
    INSERT INTO Table_2
                (X, Y)
    VALUES      (3, CAST(getdate() AS varchar(30)) )
    
    PRINT 'Insert 3'
    
    INSERT INTO Table_2
                (X, Y)
    VALUES      (4, CAST(getdate() AS varchar(30)) )
    
    SET IDENTITY_INSERT Table_2 OFF    
    
    COMMIT
    SET XACT_ABORT OFF
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have the following table: CREATE TABLE [dbo].[CampaignCustomer]( [ID] [int] IDENTITY(1,1) NOT NULL, [CampaignID]
I have the following table: CREATE TABLE [dbo].[PartsWork]( [parts_work_id] [int] IDENTITY(1,1) NOT NULL, [experiment_id]
We have the following table: CREATE TABLE [dbo].[CampaignCustomer]( [ID] [int] IDENTITY(1,1) NOT NULL, [CampaignID]
I have a table with a composite Primary Key, arranged something like this: CREATE
I am seeing too frequently not null primary key in scripts creating tables in
Why does SQL server express 2008 give me this error? CREATE TABLE model (
I just read this link http://msdn.microsoft.com/en-us/library/ms186243.aspx Here is the code to create table and
I have a organization name table with the following structure given below: CREATE TABLE
Hey guys, I have a problem with the following table: CREATE TABLE [dbo].[CA]( [Id]
I have the following schema, which I've simplified slightly: CREATE TABLE [dbo].[Header] ( [HeaderId]

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.