Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / January 2007

Tip: Looking for answers? Try searching our database.

Update Statement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
joelranck@gmail.com - 30 Jan 2007 21:03 GMT
I'm trying to update an older table with some new information. Our
previous systems stored US state info by their abbreviation, now have
a state table and link with the appropriate state id. I would like to
update the old table to our new system so set a new field called
state_id to the state id of the state abbreviation in the old table.

UPDATE accounts
SET state_id =
                      (SELECT     states.state_id
                       FROM        states, accounts
                       WHERE     states.state_abbr = accounts.state)

So something similar to above but that is not the correct syntax. I've
done this before in the past but can't remember the syntax for the
life of me.

Thanks for you help guys.
DickChristoph - 30 Jan 2007 21:22 GMT
This is not tested but I think it will work in SQL2000

update accounts
   set state_id = s.state_id
   from accounts a
   inner join states s
       on a.state = s.state_abbr
Signature

-Dick Christoph

> I'm trying to update an older table with some new information. Our
> previous systems stored US state info by their abbreviation, now have
[quoted text clipped - 13 lines]
>
> Thanks for you help guys.
joelranck@gmail.com - 30 Jan 2007 22:16 GMT
Thanks Dick, this worked.

> This is not tested but I think it will work in SQL2000
>
[quoted text clipped - 27 lines]
>
> > Thanks for you help guys.
Erland Sommarskog - 30 Jan 2007 22:32 GMT
> I'm trying to update an older table with some new information. Our
> previous systems stored US state info by their abbreviation, now have
> a state table and link with the appropriate state id.

I can't escape the comment that this does not sound like the best design
to me. Having a table for states is a good idea, but the well-established
two-letter codes are much better for keys than an id.

> I would like to update the old table to our new system so set a new
> field called state_id to the state id of the state abbreviation in the
[quoted text clipped - 9 lines]
> done this before in the past but can't remember the syntax for the
> life of me.

Just remove "accounts" from the correlated subquery, and you have an
ANSI-compliant solution.

Dick's solution works well too.

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.