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

  • Home
  • SEARCH
  • 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 4117230
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:48:31+00:00 2026-05-20T22:48:31+00:00

Here is the situation that I’m facing: I have two tables A and B.

  • 0

Here is the situation that I’m facing:

I have two tables A and B. If records are in table A and not in table B they need to be added to table B. If records are in table B and not in table A, then they need to be removed out of table B. The trick here is that it is the mixture of two keys that makes the unique combination

Table A    
Operation_Key    Part_Key  
1                1
1                2
2                1
2                3

Table B
Operation_Key    Part_Key  Record_Key
1                1         1
2                1         2
2                3         3
2                4         4

I am trying to get the right type of query so that the results returned look like

Results
Operation_Key        Part_Key  Record_Key   Action
1                    2         NULL         Add
2                    4         4            Delete

The query I have so far looks like similar to this:

CREATE TABLE #Action_Table
(
  Action VARCHAR(6),
  Action_Bit INT,
  Operation_Key INT,
  Record_Key INT,
  Part_Key INT
)
INSERT INTO #Action_Table
SELECT 
  CASE
    WHEN WS.Operation_Key IS NULL THEN 'Delete'
    WHEN WS.Operation_Key IS NOT NULL THEN 'Add'
  END Action,
  CASE
    WHEN WS.Operation_Key IS NULL THEN '0'
    WHEN WS.Operation_Key IS NOT NULL THEN '1'
  END Action_Bit,
  CASE
    WHEN WS.Operation_Key IS NULL THEN WC.Operation_Key
    WHEN WS.Operation_Key IS NOT NULL THEN WS.Operation_Key
  END Operation_Key,
  CASE
    WHEN WS.Operation_Key IS NULL THEN WC.Record_Key
    WHEN WS.Operation_Key IS NOT NULL THEN NULL
  END Workcenter_Component_Key,
  CASE
    WHEN WS.Operation_Key IS NULL THEN WC.Part_Key
    WHEN WS.Operation_Key IS NOT NULL THEN WS.Part_Key
  END Part_Key
FROM #WS_Part_Table WS
FULL OUTER JOIN #WC_Part_Table WC
  ON WC.Part_Key = WS.Part_Key
 AND WC.Operation_Key = WS.Operation_Key
WHERE (WS.Part_Key IS NULL or WC.Part_Key IS NULL) AND (WS.Operation_Key IS NULL or WC.Operation_Key IS NULL)

Both the #WS_Part_Table and the #WC_Part_Table are temp tables that I’m constructing using queries, but my dilemma is that I have to PRE-QUERY the #WC_Part_Table query on the operation key that I’m interested in, otherwise I get way too many results.

This is the query that I’m using to create the #WC_Part_Table

    CREATE TABLE #WC_Part_Table
    (
      Operation_Key INT,
      Record_Key INT,
      Part_Key INT
    )
    -- Workcenter Component Table
    INSERT INTO #WC_Part_Table
    SELECT
      O.Operation_Key,
      WC.Record_Key,
      WC.Part_Key
    FROM Workcenter_Component WC
    JOIN Operation O
      ON O.Default_Workcenter_Key = WC.Workcenter_Key

 /* There is some reason why this next line is needed */
    WHERE O.Operation_Key = 23149
  • 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-20T22:48:32+00:00Added an answer on May 20, 2026 at 10:48 pm

    Tyr this to get the results that you have posted:

    SELECT COALESCE(a.Operation_Key, b.Operation_Key) Operation_Key,
                 COALESCE(a.Part_Key, b.Part_Key) Part_Key,
                 Record_Key,
                 CASE 
                    WHEN Record_Key IS NULL THEN 'Add'
                    ELSE 'Delete'
                END Action
      FROM TableA a FULL OUTER JOIN TableB b
          ON a.Operation_Key = b.Operation_Key
            AND a.Part_Key = b.Part_Key
     WHERE (a.Operation_Key IS NULL) OR (b.Operation_Key IS NULL)
    

    Test Script:

    CREATE TABLE #TableA     
    (       
        Operation_Key INT,       
        Part_Key INT
    ) 
    
    INSERT INTO #TableA
    SELECT 1,1 
    UNION
    SELECT 1,2 
    UNION
    SELECT 2,1 
    UNION
    SELECT 2,3 
    
    CREATE TABLE #TableB
    (       
        Operation_Key INT,       
        Part_Key INT,
        Record_Key INT
    ) 
    
    INSERT INTO #TableB
    SELECT 1,1,1 
    UNION
    SELECT  2,1,2
    UNION
    SELECT 2,3,3 
    UNION
    SELECT 2,4,4 
    
    
    SELECT  COALESCE(a.Operation_Key, b.Operation_Key) Operation_Key,              
                    COALESCE(a.Part_Key, b.Part_Key) Part_Key,              
                    Record_Key,              
                    CASE                  
                        WHEN Record_Key IS NULL THEN 'Add'                 
                        ELSE 'Delete'             
                    END Action   
        FROM    #TableA a FULL OUTER JOIN #TableB b       
            ON  a.Operation_Key = b.Operation_Key         
         AND    a.Part_Key = b.Part_Key  
     WHERE (a.Operation_Key IS NULL) OR (b.Operation_Key IS NULL) 
    

    Output:

    Operation_Key   Part_Key    Record_Key  Action
    1   2   NULL    Add
    2   4   4   Delete
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting an error here that says I haven't defined a method, but it
This is beyond both making sense and my control. That being said here is
Let say I have the following desire, to simplify the IConvertible's to allow me
I have found this example on StackOverflow: var people = new List<Person> { new
There doesn't seem to be any tried and true set of best practices to
IE is giving me an undefined NAN when i try to view the calender...
I would like to remove/delete a migration file. How would I go about doing
I am writing a query to fetch results for all the values in a

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.