Solved

SQL UPDATE

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

762 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