Solved

SQL UPDATE

Posted on 2013-01-04
5
225 Views
Last Modified: 2013-01-04
Below code is only updating 1 record. Should be updating the 50 records in the temp table.

update #State
SET StateName = StateProvinceDesc
from StateProvince
WHERE  StateCode = StateProvinceCode

Not sure what the problem is.
0
Comment
Question by:thayduck
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:tim_cs
ID: 38744985
Are you sure there are 50 records that match?  Try writing it this way.

Update s
SET s.StateName = sp.StateProvinceDesc
FROM #State s
   INNER JOIN StateProvince sp
      ON s.StateCode = sp.StateProvinceCode
0
 

Author Comment

by:thayduck
ID: 38745028
#State
AL      USA      AL-USA      Alabama
AR      USA      AR-USA      AR-USA
AZ      USA      AZ-USA      AZ-USA

StateProvince
2      AK         Alaska              USA
1      AL         Alabama              USA
3      AR         Arkansas              USA
4      AZ         Arizona              USA
5      CA         California         USA
6      CO         Colorado              USA
7      CT         Connecticut      USA

You get same results as me, 1 record updated, Alabama.
0
 
LVL 15

Accepted Solution

by:
tim_cs earned 100 total points
ID: 38745072
What are the datatypes for the fields, do they match?  Is it always just Alabama that gets updated?

What happens if you run this one?  

Update s
SET s.StateName = sp.StateProvinceDesc
FROM #State s
   INNER JOIN StateProvince sp
      ON s.StateCode = sp.StateProvinceCode
      AND s.StateName <> sp.StateProvinceDesc
0
 

Author Comment

by:thayduck
ID: 38745103
I am re-keying the State Province records into the StateProvince table.
I re-keyed 4 records and reran query. Now those records are being updated correctly.
Visually they all look right, so I am not sure why re-keying over the state code in the table is  causing the records to now be updateable. ???????????????????????????????????????????????

I will let you know if this is all fixed after I re-key all records.
0
 

Author Closing Comment

by:thayduck
ID: 38745342
Re-Keyed records and everything is fine now.

Last time I do a copy and paste when creating records in a table.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
monitoring configuration for SQL server DB 32 60
Insert msdb.dbo.sysmail_event_log Process_ID into table 4 57
SQL- GROUP BY 4 46
EF5: Update Model from Database not working 3 69
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question