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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:40:17+00:00 2026-05-16T18:40:17+00:00

I have a poorly normalized set of tables and I am trying to correct

  • 0

I have a poorly normalized set of tables and I am trying to correct this issue. The DB is MS SQL 2005.

Table1 has an id field (record ID), a mainID field (person ID) and a sequence field (and int that determines the sort order)

Table2 has its own id field and a copy of the id for the first record by a person (sequence = 1).

I’ve added a new field to table2 call table2_id and I would like to populate this field with the ID from table2 so that I can do away with table1.mainID. table2 only has a record for one of the records for each person and mainId is set to the id where table1.sequence = 1.

This is the update query I thought would do the job by I’m getting errors

update table1 as a  
set regInfoID = (select b.id 
                 from table2 as b 
                 where b.ref1 = (select c.id 
                                 from table1 as c 
                                 where c.mainID = a.mainID 
                                       and sequence = 1))  

I believe I’m on the right track her since the following query works fine

select regInfoID = (select b.id 
                    from table2 as b 
                    where b.ref1 = (select c.id 
                                    from table1 as c 
                                    where c.mainID = a.mainID 
                                          and sequence = 1)), a.*  
from table1 as a  
  • 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-16T18:40:18+00:00Added an answer on May 16, 2026 at 6:40 pm

    I think your query is equivalent to this:

    update a
    set regInfoID = b.id
    -- select a.*, b.id
    from table2 b 
    inner join table1 c on c.id = b.ref1
    inner join table1 a on c.mainID = a.mainID and c.sequence = 1
    

    From this query, I think you will potentially have indeterminate results because table2 (b) is not guaranteed to be a single row result. So regInfoID will be set to one of the resulting b.id values.

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

Sidebar

Related Questions

So we have a piece of software which has a poorly written SQL statement
I'm trying to use Doctrine with a poorly designed PostgreSQL database. Some tables have
Say we have some poorly written SQL that does not release temp tables when
I have a poorly designed database. One of the most important tables has 11,000+
edit: my table schema below is poorly normalized as suggested and I have revamped
I've decided to rewrite a database I have that is poorly normalized. I've created
I have a huge database that has lots and lots of poorly documented constraints.
We have had an issue with a block of code that responds poorly in
I have the CakePHP Comments Plugin set-up correctly, I believe. I've followed the poorly-written
I have an assignment to tune a poorly performing query running on a SQL

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.