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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:08:54+00:00 2026-06-03T12:08:54+00:00

I have the below compare sql code. Both tables contain a column IDCodeField. How

  • 0

I have the below compare sql code.
Both tables contain a column IDCodeField.
How can I compare only IDCodeField, but return all fields if match IDCodeField?

Currently I am using below, however, it will compare all fields instead of IDCodeField only.

ALTER PROCEDURE [dbo].[usp_comparetables](@table1 varchar(100), 
 @table2 Varchar(100), @columnlist Varchar(1000))

AS

DECLARE @sql VARCHAR(8000)
SET @sql = 
'SELECT ''' + @table1 + ''' AS DataState, * FROM
    (SELECT ' + @columnlist + ' FROM ' + @table1 + '
        EXCEPT
    SELECT ' + @columnlist + ' FROM ' + @table2 + ') x
UNION
SELECT ''' + @table2 + ''' AS DataState, * from
    (SELECT ' + @columnlist + ' FROM ' + @table2 + '
        INTERSECT
    SELECT ' + @columnlist + ' FROM ' + @table1 +') x'

EXEC(@sql)

Used Answer:

DECLARE @sql VARCHAR(8000)
SET @sql = 
'SELECT ''' + @table1 + ''' AS DataState, '+@columnlist+' FROM ' + @table1 + ' where '+@compareparameter+' not in (select '+@compareparameter+' from '+@table2+') 
UNION ALL 
SELECT ''' + @table2 + ''' AS DataState, '+@columnlist+' FROM ' + @table2 + ' where '+@compareparameter+' not in (select '+@compareparameter+' from '+@table1+')' 
EXEC(@sql)
  • 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-03T12:08:55+00:00Added an answer on June 3, 2026 at 12:08 pm

    I think you want all rows from Table1 and Table2, such that each IDCodeField values only appears in one of the tables or the other. You wish to exclude rows where the same value appears in both tables.

    Ignoring, for the moment, the question of what to do if the same value appears in the same table, the simplest query would be:

    SELECT * from Table1 T1 full outer join Table2
    ON T1.IDCodeField = T2.IDCodeField
    WHERE T1.IDCodeField is null or T2.IDCodeField is null
    

    This will give you the results, but possibly not in the format you’re seeking – the result rows will be as wide as both tables combined, and the columns from the non-matching table will be NULL.

    Or, we could do it in the UNION style from your question.

    SELECT * from Table1 where IDCodeField not in (select IDCodeField from Table2)
    UNION ALL
    SELECT * from Table2 where IDCodeField not in (select IDCOdeField from Table1)
    

    Both of the above queries will return rows if the same IDCodeField value is duplicated only within a single table. If you wish to exclude this possibility, you might try finding the unique values first:

    ;With UniqueIDs as (
        SELECT IDCodeField
        FROM (
            SELECT IDCodeField from Table1
            union all
            select IDCodeField from Table2) t
        GROUP BY IDCodeField
        HAVING COUNT(*) = 1
    )
    SELECT * from (
        SELECT * from Table1
        union all
        select * from Table2
    ) t
      INNER JOIN
    UniqueIDs u
      ON
        t.IDCodeField = u.IDCodeField
    

    (Of course, all the uses of SELECT * above should be replaced with appropriate column lists)

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

Sidebar

Related Questions

In my .jsp file I have the code below. How can I compare the
I wrote below code to compare to arrays that have same elements but in
I have this code below where I loop through string and compare everything char
I have designed a stored procedure usign Sql Server 2005 below to compare 3
I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
I have below html code in my aspx. <input type=hidden id=medicalLink value='<a href=/forms/contactus.aspx >Contact
I have below code in html. <li class=selected runat=server id=lihome><a href=/ISS/home.aspx title=Home><span>Home</span></a></li> Now I
I have below is the html code for TD which gets appended after matching
I have below code behind in c# if (Session[cmpDictionaryTitle]!= null) { downloadLinks.Text += @<li><a
I have below code to insert a style into DOM (there is a use

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.