Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
nick_dkc
Starting Member
25 Posts |
Posted - 2009-10-21 : 03:19:57
|
| My DB is for pet animals and one of the requirements is to use data from standard lists of species and breeds to create a variable 'breedName' for each pet, which shows if the animal is a purebreed or a cross breed, e.g.a cross breed dog could have breedName = "Alsation X Labrador"a pure breed dog could have breedName = "Labrador"a bird cannot have a crossbreed, so e.g. breedName = "African Grey Parrot"The following tables are involved:dbo.speciesspeciesID speciesName1 Dog2 Cat3 Bird4 Rabbit5 Hamster6 Ferretdbo.breedsbreedID speciesID breedName1 1 Labrador2 1 Alsatian3 2 Maine Coon4 2 Domestic Short Hair5 3 African Grey Parrot6 3 Falcondbo.petDetailspetID (INT)speciesID (INT)crossed (BIT) - this is either true or false to show if the animal is a crossbreedbreedPureID (INT)breedCrossID (INT)breedName (nvarchar(50)) - the column I want to populatepetID speciesID Cross breedPureID breedCrossID breedName (desired results)1 1 1 1 2 Labrador X Alsatian2 2 0 3 NULL Maine Coon3 3 0 5 NULL African Grey Parrot4 4 0 NULL NULL RabbitWhat I need help with is an insert/update trigger that gets the value of 'breedPureID' and 'breedCrossID' from dbo.breeds, concatenates the results and populates the 'breedName' column in petDetails. I think it should be something like this but I don't know how to get the values from the dbo.breeds for both breedpureID and breedCrossID:UPDATE dbo.PetDetailsSET breedName =CASEWHEN speciesID < 3 AND crossed = 1 THEN 'value of breedPureID' + ' X ' + 'value of breedCrossID'WHEN speciesID < 3 AND crossed = 0 THEN 'value of breedPureID'WHEN speciesID = 3 then 'value of breedPureID'WHEN speciesID > 3 then ' 'ENDCan anyone help? Pretty Please?? Woof Woof? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-21 : 12:10:33
|
| [code]update pdset pd.breedName=coalesce(coalesce(b1.breedName+ 'X','') + coalesce(b2.breedName,''),s.speciesName)from dbo.petDetails pdjoin dbo.species son s.speciesID=pd.speciesIDleft join dbo.breeds b1on b1.speciesID=s.speciesIDand b1.breedID=pd.breedPureIDleft join dbo.breeds b2on b2.speciesID=s.speciesIDand b2.breedID=breedCrossID[/code] |
 |
|
|
nick_dkc
Starting Member
25 Posts |
Posted - 2009-10-22 : 01:17:31
|
| Many thanks!Great support in this forum! |
 |
|
|
nick_dkc
Starting Member
25 Posts |
Posted - 2009-10-22 : 01:17:31
|
| Many thanks!Great support in this forum! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-22 : 12:47:50
|
| welcome |
 |
|
|
|
|
|