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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:40:33+00:00 2026-06-12T09:40:33+00:00

I have the following situation where a stored procedure gathers data and performs the

  • 0

I have the following situation where a stored procedure gathers data and performs the necessary joins and inserts the results into a temp table (ex:#Results)

Now, what I want to do is insert all the records from #Results into a table that was previously created but I first want to remove (truncate/delete) the destination and then insert the results. The catch is putting this process of cleaning the destination table and then inserting the new #Results in a transaction.

I did the following:

BEGIN TRANSACTION

DELETE  FROM PracticeDB.dbo.TransTable

IF @@ERROR <> 0 
    ROLLBACK TRANSACTION
ELSE 
    BEGIN

        INSERT  INTO PracticeDB.dbo.TransTable
                ( 
                   [R_ID]
                  ,[LASTNAME]
                  ,[FIRSTNAME]
                  ,[DATASOURCE]
                  ,[USER_STATUS]
                  ,[Salary]
                  ,[Neet_Stat]
                )
                SELECT  [R_ID]
                      ,[LASTNAME]
                      ,[FIRSTNAME]
                      ,[DATASOURCE]
                      ,[USER_STATUS]
                      ,[Salary]
                      ,[Neet_Stat]
                FROM    #RESULT

        Select @@TRANCOUNT TransactionCount, @@ERROR ErrorCount
        IF @@ERROR <> 0 
            ROLLBACK TRANSACTION

        ELSE 
            COMMIT TRANSACTION

    END

but I know it isn’t working properly and I’m having a hard time finding an example like this though I dont know why considering it seems like something common. In this case it still deletes the target table though the insert fails.

More than anything, some guidance would be nice as to best approach this situation or best practices in a similar case (whats best to use and so forth). Thank you in advance…

  • 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-12T09:40:34+00:00Added an answer on June 12, 2026 at 9:40 am

    I’m really not seeing anything wrong with this. So it DOES delete from your TransTable, but doesn’t do the insert? Are you sure #RESULT has records in it?

    The only thing that I see, is you’re checking @@ERROR after Select @@TRANCOUNT TransactionCount, @@ERROR ErrorCount, which means @@ERROR is going to be from your SELECT statement and not the INSERT statement (although I would always expect that to be 0).

    For more info on @@ERROR, see: http://msdn.microsoft.com/en-us/library/ms188790.aspx

    You should check @@ERROR after each statement.

    As far as best practices, I think Microsoft now recommends you use TRY/CATCH instead of checking @@ERROR after each statement (as of SQL 2005 and after). Take a look at Example B here: http://msdn.microsoft.com/en-us/library/ms175976.aspx

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

Sidebar

Related Questions

I have the following situation: I have a java code launching a stored procedure.
I have a stored procedure that performs a bulk insert in a table. I
I have following situation, String a=<em>crawler</em> <em> Yeahhhhh </em></a></h3><table; System.out.println(a.indexOf(</em>)); It returns the 11
I have following Situation, Server A sends some data (HTML form) to server B,
I have the following situation. We want an reputation table to evaluate Users And
Background: There's a stored procedure which does stuff with a temp table of a
I have the following situation. Project files was stored in ~/django-apps/app-old/app. For some reasons
I am solving in these days following situation: In my DB table I have
I have the following situation: the user submits a form, I fetch the data
Say I have the following situation (for demonstration). The simple table content is converted

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.