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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:20:33+00:00 2026-06-03T03:20:33+00:00

I have the below sql query: select @table1 as DataState, * from (select *

  • 0

I have the below sql query:

select @table1 as DataState, *  from
(select * from importedcsvclients 
except 
select * from tblClients) x
union all
select @table2 as DataState, *  from
(select * from tblClients 
except select *  
from importedcsvclients) x

The above code works fine, however, if table1 and table2 consist similar data, both records will show.

Can anyone assist me to make the query work as:

get the results of both table1 and table2, but only show table2 data if the same name doesn’t exist in table1.

Thanks.

For Info:

table1
ID   Name
1    TestA
2    TestB    
3    TestC
4    TestD

table2
ID   Name
1    TestE
2    TestF    
3    TestG
4    TestD

Results:
Name     DataState
TestA    table1
TestB    table1
TestC    table1
TestD    table1
TestE    table2
TestF    table2
TestG    table2
  • 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-03T03:20:34+00:00Added an answer on June 3, 2026 at 3:20 am

    I used the below now.
    Thanks for all assistance.

    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) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a query like the one below: SELECT t1.id, t1.Name FROM Table1
I have an SQL query (below) that essentially takes a student from tbStudents, and
I'm using Sql-Server, and I have the below query which returns all columns that
I have the code below: string SQL = select * from + TableName; using
I have the below sql query to insert the records from a table. The
i have a sql query in asp.net SqlCommand QueidCmd = new SqlCommand(select scope_identity() from
I have a SQL Query as below: SELECT urban_appr, urban_in, rural_appr, rural_in, ground_appr, ground_in
I have the below SQL query: SELECT g.name AS gname, COUNT(u.id) AS noMembers, f.name
I have a Pl/SQL query that pull all records from the 3 tables. That
I have a SQL query which retrieve wall post list $qry=mysql_query(SELECT senderid,post,date FROM .post_table.

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.