Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Using Update...Set to update a table

 
   Database Forums (Home) -> MSEQ RSS
Next:  Permission required to execute embedded assembly?  
Author Message
Dan112

External


Since: Aug 24, 2004
Posts: 69



(Msg. 1) Posted: Thu Oct 04, 2007 7:54 pm
Post subject: Using Update...Set to update a table
Archived from groups: microsoft>public>sqlserver>mseq (more info?)

I have a table (we'll call it table A) with two fields in it; one is an ID,
and the other (named "State") currently only contains null values. I'd like
to update the "State" field with values from another table in the database
(table B). The two databases share the same ID field, so I can join them
using these two values.

Below is what I've tried to run so far, but it's not working. My problem is
that I'm getting multiple records from my subquery, so I can't use "=". I
think I'm heading down the right track, but I can't figure this out. Any help
you guys can provide will be well-appreciated.

Thanks, Dan



set transaction isolation level read uncommitted

Update A
Set State = (select B.State from B with (nolock) join A with (nolock) on
B.ID=A.ID)

 >> Stay informed about: Using Update...Set to update a table 
Back to top
Login to vote
Russell Fields

External


Since: Feb 21, 2007
Posts: 457



(Msg. 2) Posted: Fri Oct 05, 2007 9:57 am
Post subject: Re: Using Update...Set to update a table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dan,


Since you apparently have a single row in table A that matches more than 1
row from table B. This means that you have to choose to get only a single
value. (It is possible that all values for State in table B that have the
same ID are identical, but the SQL Server will not assume that.) So, let's
say that MAX is good enough.

This will not fail, but the answer will be wrong, because it would set every
row in A to the same maximum State that existed in the join between A and B,
which is not what you wanted, of course.

Update A
Set State = (select MAX(B.State) from B with (nolock) join A with (nolock)
on
B.ID=A.ID)

That is because the join is all in the subselect and is not correlated to
the row in A that you wish to update. If you remove the join from the
subselect and use the WHERE clause to refer to the table A in the Update,
you would get something like this:

Update A
Set State = (select MAX(B.State) from B with (nolock) WHERE B.ID=A.ID)

Actually, if the rows only match IDs 1 to 1, then you do not even need the
MAX any more, but assuming that it is not that simple, I prefer using a
derived table, as:

UPDATE A
SET A.State = C.State
FROM A JOIN
(SELECT ID, MAX(State) AS State
FROM B
GROUP BY ID) AS C
ON A.ID = C.ID

All the best,

RLF

"Dan" wrote in message

>I have a table (we'll call it table A) with two fields in it; one is an ID,
> and the other (named "State") currently only contains null values. I'd
> like
> to update the "State" field with values from another table in the database
> (table B). The two databases share the same ID field, so I can join them
> using these two values.
>
> Below is what I've tried to run so far, but it's not working. My problem
> is
> that I'm getting multiple records from my subquery, so I can't use "=". I
> think I'm heading down the right track, but I can't figure this out. Any
> help
> you guys can provide will be well-appreciated.
>
> Thanks, Dan
>
>
>
> set transaction isolation level read uncommitted
>
> Update A
> Set State = (select B.State from B with (nolock) join A with (nolock) on
> B.ID=A.ID)

 >> Stay informed about: Using Update...Set to update a table 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Update 'Date': Set Day(Date) to a Certain Number - Hi, I am trying to use an update command to set the day of a date to a certain number. For example, if the date is 1/4/06, I might want to set day(date) to '7'. So it turns into 1/7/06. I wasn't sure if there was a way to do this and I keep getting....

Table Query - I have a table that has the following three fields and values that I am trying to build a query on; Column name-FEDERAL, FICA1, FICA2 Values- 1 , 100 , 200 2 , 300 , 400 How can I get a query to...

backup table to another table in database - I need to create a new table with the same columns using a select from another table. I cannot find the syntax. Does anyone know if this can be done in SQL 2k? -- Danne

Doing multiple joins on one table - I have those Tables: CREATE TABLE GameYellows( id INT NOT NULL AUTO_INCREMENT, AddedDate DATETIME NOT NULL, Player_ID INT NOT NULL, ) CREATE TABLE GameReds( id INT NOT NULL AUTO_INCREMENT, AddedDate DATETIME NOT NULL, Player_ID INT NOT NULL, ) ...

sql query single table - I am trying to run a report. The requestor has specifics on the way it should be formatted on output. I'm going to try and explain what he's looking for as well as give the example of output which was given me. The "Central Host" is the s...
   Database Forums (Home) -> MSEQ All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]