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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:57:11+00:00 2026-06-18T17:57:11+00:00

I have written a complex stored procedure that moves records between several tables based

  • 0

I have written a complex stored procedure that moves records between several tables based on complex logic. I have checked each little piece of the logic carefully but I want to have maximal confidence in the code. Is there a way to declare a logical relationship that must hold between the beginning and end of the SP? If these conditions are not met, I imagine that the SP will be rolled back.

Specifically, I want to declare that records in table A that are picked out by a set of logical conditions will be in table B (and not in table A) at the end of the SP and that records not picked out by those set of logical conditions will still be in table X at the end of the SP. I use a boolean function to pick out the records in A that match the conditions.

I know that I can test some of these things with nUnit but I am wondering if there is a way to declare and enforce this sort of logic within t-SQL itself.

  • 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-18T17:57:12+00:00Added an answer on June 18, 2026 at 5:57 pm

    If your records have a field containing a unique ID you can do a simple INSERT + SELECT from TableA into TableB and then a simple DELETE of the inserted original records.

    For instance first insert all the records matching your selection criteria into TableB:

    INSERT INTO TableB (uniqueID, Field1, Field2, FieldN)
    SELECT uniqueID,
           Field1,
           Field2,
           FieldN
    FROM   TableA
    WHERE  FieldN = SomeCriteria
    

    And then delete from TableA all the records you just inserted into tableB using the field uniqueID as the selction criteria to determine which records to delete:

    DELETE TableA
    WHERE  uniqueID IN (SELECT uniqueID
                        FROM   TableB)
    

    If you put both statements in a single transaction with a couple error checks you should be protected in case something goes wrong while the two statements are executing:

    BEGIN TRANSACTION
    
    INSERT INTO TableB (uniqueID, Field1, Field2, FieldN)
    SELECT uniqueID,
           Field1,
           Field2,
           FieldN
    FROM   TableA
    WHERE  FieldN = SomeCriteria;
    
    IF @@ERROR <> 0 THEN
    BEGIN
        ROLLBACK TRANSACTION
        RETURN (@@ERROR)
    END
    
    DELETE TableA
    WHERE  uniqueID IN (SELECT uniqueID
                        FROM   TableB);
    if @@ERROR <> 0 THEN
    BEGIN
        ROLLBACK TRANSACTION
        RETURN (@@ERROR)
    END
    
    COMMIT TRANSACTION 
    

    If you do not have a single column which can unique identify each of your records you can use EXISTS instead of IN for selecting the records to DELETE from TableA:

    DELETE TableA
    WHERE  EXISTS (SELECT *
                   FROM   TableB
                   WHERE  TableA.field1 = TableB.field1
                          AND TableA.field2 = TableB.field2
                          AND TableA.FieldN = TableB.fieldn);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a complex stored procedure that has to return Sum of AmountSold as
I have to modify a WPF application written in C# that displays a complex
I have recently written some complex ruby script that eventually fails with segfaults in
I have written a piece of code that uses complex library. I have put
I have written a mysql stored procedure in my magento database to perform a
I have written complex library for managing network communication based on iocp mechanism. Problem
I have written a very complex database migration script in Groovy, that runs just
I have written a complex query that will return me a list of IDs.
I have written several program and found out that when compiled in 64bit, the
I have code written in c, which contains some complex algorithms and I want

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.