Forum Moderators: open

Message Too Old, No Replies

SQL Statement Help to Normalize a DB

Using Query to replace descr with id

         

Argblat

3:19 pm on May 19, 2006 (gmt 0)

10+ Year Member



In attempting to clean up a poorly designed database structure I've run into a snag. I will try to use a simplified example to help illustrate:

TABLE ACCOUNTS [acctID, acctRep]
1 ¦ BOB
2 ¦ SALLY
3 ¦ JIM
4 ¦ BOB

In order to normalize the table I moved the Account Rep's to their own table as such:

TABLE ACCOUNT_REP [repID, repName]
1 ¦ BOB
2 ¦ SALLY
3 ¦ JIM

I now need to, using update query, update table ACCOUNTS to change acctRep from a name to an ID. Can someone please help me with the query using the newly created link table to update the master table?

-Mike

Argblat

4:15 pm on May 19, 2006 (gmt 0)

10+ Year Member



I got it,

UPDATE ACCOUNTS
SET ACCOUNTS.acctRep = ACCOUNT_REP.repID
FROM ACCOUNTS
INNER JOIN ACCOUNT_REP
ON ACCOUNT_REP.repName = ACCOUNTS.acctRep

syber

9:50 pm on May 19, 2006 (gmt 0)

10+ Year Member



This would be the ANSI syntax


UPDATE ACCOUNTS
SET acctRep = (SELECT repID FROM ACCOUNT_REP
WHERE repName = ACCOUNTS.acctRep)

piatkow

2:09 pm on May 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In a business environment I wouldn't risk making that change on the fly. Output to a temporary table and eyeball the results first. Once you are happy that there are no problems delete the original and rename the temporary table.