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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:31:48+00:00 2026-06-02T23:31:48+00:00

I had the below code, but it is showing all records due to the

  • 0

I had the below code, but it is showing all records due to the UNION:

'WITH x AS
( 
  SELECT m, ' + @columnlist + ', rn = ROW_NUMBER() OVER (PARTITION BY ClientNric ORDER BY m DESC) 
  FROM  
  (   
    SELECT m = ''' + @table1 + ''', * FROM ' + @table1 + '
    UNION ALL  
    SELECT m = ''' + @table2 + ''', * FROM ' + @table2 + '
  ) AS y 
) 
SELECT ' + @columnlist + ', DataState = m 
  FROM x  
  WHERE rn = 1;'

exec(@sql)

I need the results as follows:

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    table2 

Can advise how to achieve the results?
Thanks.

  • 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-02T23:31:50+00:00Added an answer on June 2, 2026 at 11:31 pm

    A left join should do the job, I think:

    SELECT
      t1.Name,
      DataState = COALESCE(t2.DataState, 'table1')
    FROM table1 t1
      LEFT JOIN (
        SELECT
          Name,
          DataState = 'table2'
        FROM table2
      ) t2 ON t1.Name = t2.Name
    

    Or maybe the join should be on the ID column, I’m not sure. In any event, this should at least give you the idea.

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

Sidebar

Related Questions

How to Remove Badge from the tabbar item i had use below code but
I had used the below code to get the row index of the UIPickerView
I had a weird problem here. Please check the code below. I do not
Answer solved in edit below I had this piece of code Dictionary<Merchant, int> remaingCards
I am told that the configuration below is possible, but have had significant difficulty
I'm showing the code to this problem for example purposes, but really my question
Here i had added code below in which i had ArrayList Which contains values
I had used the below mention code for a custom left pointed back button
I asked this question before, but had a hard time showing which edits I
I've been trying to bind events to jquery objects (code below) but its really

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.