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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:25:40+00:00 2026-05-23T17:25:40+00:00

If I have the following SQL query CREATE TABLE #t1 (a INT NOT NULL

  • 0

If I have the following SQL query

CREATE TABLE #t1
    (a INT NOT NULL PRIMARY KEY);
SET XACT_ABORT ON
go

BEGIN TRANSACTION
INSERT INTO #t1 VALUES (1);
INSERT INTO #t1 VALUES (2);
INSERT INTO #t1 VALUES (3);
INSERT INTO #t1 VALUES (2); -- PK violation error
go

INSERT INTO #t1 VALUES (4);
go

COMMIT TRANSACTION

SET XACT_ABORT OFF

When I run the query I get this output

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected) Msg 2627, Level 14, State 1, Line 7 Violation of
PRIMARY KEY constraint ‘PK_#t1______66D536B1′. Cannot insert
duplicate key in object ‘dbo.#t1’.

(1 row(s) affected) Msg 102, Level 15, State 1, Line 8 Incorrect
syntax near ‘#t1’.

And the value of #t1 is

a
-----------
4

What do I need to do so the script will abort out and not run the second batch?

EDIT:

Tried JNK’s solution

begin try
BEGIN TRANSACTION
INSERT INTO #t1 VALUES (1)
INSERT INTO #t1 VALUES (2)
INSERT INTO #t1 VALUES (3)
INSERT INTO #t1 VALUES (2) -- PK violation error
go

INSERT INTO #t1 VALUES (4)
go

COMMIT TRANSACTION

end try
begin catch 
IF @@trancount > 0 Rollback

END catch

select * from #t1

truncate table #t1

However I get this output

Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ')'.

(1 row(s) affected)
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'begin'.
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near 'END'.

And the table still contains a 4 afterward

EDIT2:

the errors with the try catch is because of the GO statement, which I need to have for my real script instead of this test case. so it appears try/catch will not work for this case.

  • 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-05-23T17:25:41+00:00Added an answer on May 23, 2026 at 5:25 pm

    Add error handling:

    BEGIN TRY
    <your query>
    END TRY
    BEGIN CATCH
    
    IF @@TRANCOUNT > 0 ROLLBACK    
    
    <other error reporting processes>
    
    END CATCH
    

    The ROLLBACK is optional and will undo any inserts that completed before the error occured. You can also add multiple transactions, so if say the first 3 inserts are independent of some other stuff later on, wrap those in a transaction and commit it, then have another tran later on for the unrelated process.

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

Sidebar

Related Questions

I have following table and data: create table Foo ( id int not null,
I have a table like so.. CREATE TABLE [dbo].[Locations_Hours]( [LocationID] [int] NOT NULL, [sun_open]
Hello, I have the following sql table + data: CREATE TABLE IF NOT EXISTS
I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have a table like this one CREATE TABLE MyTable ( [MyTableID] [int] NOT
I have a following table in SQL Server 2008 database: CREATE TABLE [dbo].[Actions]( [ActionId]
CREATE TABLE `pastebin` ( `pid` int(11) NOT NULL auto_increment, `poster` varchar(16) default NULL, `posted`
I have a SQL Server table with the following structure: CREATE TABLE [dbo].[Log]( [LogID]
I have the following very large table in SQL Server 2005: create table Blah
I have the following sql query for transforming data but is it possible to

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.