1

I'm going to use example tables.

tblAnimal:

---------------------------------
|Animal    | Colour |  Gender   |
---------------------------------
|Dog       |        |           |
---------------------------------

tblDescription:

-------------------------------
| Animal |  ID  | Description |
-------------------------------
|  Dog   |   1  |    Male     |
-------------------------------
|  Dog   |  92  |   White     |
-------------------------------

So I want to update tblAnimal so that the Colour and Gender are populated.

I used the code

UPDATE tblAnimal
SET Colour = CASE WHEN tblDescription.ID = 92 THEN tblDescription.Description END,
    Gender = CASE WHEN tblDescription.ID = 1 THEN tblDescription.Description END
FROM tblDescription INNER JOIN tblAnimal 
    ON tblDescription.Animal = tblAnimal.Animal

But it isn't working. Both columns in tblAnimal remains NULL. It seems like SQL Server only checks the first row in tblDescription.

How would I write this UPDATE statement so that both Colour and Gender are what is in tblDescription?

EDIT

Thank you all for your solutions! And yes the table needs to be normalized, but this is strictly for example to go along with my question on the UPDATE statement.

Thanks again everyone.

4 Answers 4

1
Create table tblDescription(
Animal varchar(20),
ID int,
Description varchar(20)
)

Insert into tblAnimal(Animal)
values ('Dog')

Insert into tblDescription
Select 'Dog',1,'Male' union all
Select 'Dog',92,'White'

Select * from tblAnimal

Select * from tblDescription

---Update

Update a
SET Colour = (Select d.description from tbldescription d where d.id =92),
Gender = (Select d.description from tbldescription d where d.id =1)

From tblAnimal a inner join
tblDescription d on d.Animal =a.Animal

NOTE: This will work but what would i do is normalization of tbldescription.

1

Try this :

UPDATE tblAnimal a
SET
    a.Colour = (SELECT d.Description d FROM tblDescription d WHERE d.Animal = a.Animal AND ID = 92),
    a.Gender = (SELECT d.Description d FROM tblDescription d WHERE d.Animal = a.Animal AND ID = 1)
0

You need to have a ID reference in tblAnimal in order to get correlation with tblDescription.

tblAnimal:

---------------------------------
|Animal | ID   | Colour |  Gender|
---------------------------------
|Dog    |  1  |         |       |
---------------------------------
|Dog    |  92 |         |       |
---------------------------------

Then you could run this:

UPDATE 
  tblAnimal as a
SET
  Colour = (SELECT Description from tblDescription x WHERE x.id = a.id),
  Gender = (SELECT Description FROM tblDescription x WHERE x.id = a.id)
2
  • Probably I couldn't understand the problem. My answer assumed a one-to-one relationship between tables. It is probable that @rida-benhammane 's answer is the correct one. Feb 7, 2014 at 15:21
  • @go_55699, Here is the SQL Fiddle that shows that this approach does indeed work.
    – Linger
    Feb 7, 2014 at 15:28
0

Another way:

UPDATE tblAnimal
    SET colour = (select  Description from  tblDescription where ID = 92),
        Gender = (select  Description from  tblDescription where ID = 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.