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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:17:20+00:00 2026-06-06T09:17:20+00:00

I have 2 tables Table A NameID FirstName MiddleName LastName Addr1 Addr2 Phn1 Phn2

  • 0

I have 2 tables

Table A

NameID  FirstName  MiddleName  LastName  Addr1  Addr2  Phn1  Phn2  City  State 
NULL      Micheal    Calvin      Dodson    12     23     1234  123  XYZ   ABC    
NULL      John       NULL        Keith     NULL   NULL   2344  NULL SQE   FDG    
NULL      John       NULL        Keith     NULL   NULL   2344  NULL SQE   FDG 
NULL      William    Stephen     NULL      45     NULL   NULL  NULL HJD   ABC
NULL      Victor     NULL        Anthony   NULL   NULL   NULL  NULL NULL  NULL

Table B

NameID   FirstName  MiddleName  LastName  Addr1  Addr2  Phn1  Phn2  City State Zip Email Gender...

I need to get the distinct records of (FirstName,MiddleName,LastName) of Table A and insert the same details along with the other fields matching with Table A into Table B.
My Table B has NameID as an identity coloum. So after inserting a unique record into Table B, I need to get that NameID and insert it back into Table A shown below :

TABLE A

Table A

NameID  FirstName  MiddleName  LastName  Addr1  Addr2  Phn1  Phn2  City  State 
1         Micheal    Calvin      Dodson    12     23     1234  123  XYZ   ABC    
2         John       NULL        Keith     NULL   NULL   2344  NULL SQE   FDG    
2         John       NULL        Keith     NULL   NULL   2344  NULL SQE   FDG 
3         William    Stephen     NULL      45     NULL   NULL  NULL HJD   ABC
4         Victor     NULL        Anthony   NULL   NULL   NULL  NULL NULL  NULL

TABLE B

NameID  FirstName  MiddleName  LastName  Addr1  Addr2  Phn1  Phn2  City State Zip  Email Gender...
1       Micheal    Calvin      Dodson    12     23     1234  123   XYZ   ABC   NULL NULL  NULL
2       John       NULL        Keith     NULL   NULL   2344  NULL  SQE   FDG   NULL NULL  NULL
3       William    Stephen     NULL      45     NULL   NULL  NULL  HJD   ABC   NULL NULL  NULL
4       Victor     NULL        Anthony   NULL   NULL   NULL  NULL  NULL  NULL  NULL NULL  NULL

Can you please help me with this. Im not able to get this query right. Code in SQL Server 2008

Thanks in advance,
Sunitha

  • 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-06T09:17:21+00:00Added an answer on June 6, 2026 at 9:17 am

    I think the easiest way to do this is with two queries. The first problem is handling duplicates in TableA. The following query selects an arbitrary row for each name combination:

    insert into TableB()
    select ()
    from (select a.*,
    row_number() over (partition by FirstName, MiddleName, LastName order by FirstName) as seqnum
    from TableA a
    ) a
    where seqnum = 1

    Then, update the original table:

    update TableA
        set NameId = (select max(NameId) from TableB
                      where TableB.FirstName = TableA.FirstName and
                            TableB.MiddleName = TableA.MiddleName and
                            TableB.LastName = TableA.LastName
                     )
        where NameId is null
    

    If your fields contain NULL values (rather than blanks), you can use coalesce() for the join conditions:

    update TableA
        set NameId = (select max(NameId) from TableB
                      where coalesce(TableB.FirstName, '<null>') = coalesce(TableA.FirstName, '<null>') and
                            coalesce(TableB.MiddleName, '<null>') = coalesce(TableA.MiddleName, '<null>') and
                            coalesce(TableB.LastName , '<null>')= coalesce(TableA.LastName, '<null>')
                     )
        where NameId is null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables Table NAME (id, name, phone, city, state, rid) Table NAMES2
I have a table was named MYTABLE. That have two columns FIRSTNAME and LASTNAME.
I have two tables: CREATE TABLE [NEWS] ( [ID] INT IDENTITY(1,1) NOT NULL, [TITLE]
I have one table named tblm_customer . It contain field named firstname and lastname
I have 2 tables, let says the first table named Schedule and the second
So I have two tables. Table 1 is named 'appointment' with the following fields:
i have 3 tables A,B and C. table A has column employee_name,id table B
i have 2 tables, users and follows. table follows has a column named status.
I have many tables, say T1, T2, T3. Every table has a column named
I have a Name table with the columns NameID Name TypeID With the following

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.