Solved

SQL UPDATE

Posted on 2013-01-04
5
222 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
  • 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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 Backup & Restore 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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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