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 |
|
henrikop
Constraint Violating Yak Guru
280 Posts |
Posted - 2005-12-14 : 10:11:16
|
| I'm working with SQL 2005.I have a table, I want one of the columns to fill with a default value depending on another column in that table.So if a recordtable contains flowers and a record is filled withFlowerType "Rose" I want FlowerColor to be "Red"FlowerType "Tulip" I want FlowerColor to be "Purple"(I have a table FlowerType where I have a defaultColor column).Assume that I want to solve this with a trigger or a default function.How do I do that?Henri~~~~The envious praises me unknowingly |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2005-12-14 : 11:02:53
|
| I assume ur Table is Flower and it has 2 columns FlowerType & FlowerColor(I'm a bit confused with ur wording -- FlowerType Table? Data in FlowerType is "Rose", "Tulip" .... )U can write a trigger, to be triggered when u insert data to that tableUpdate Inserted set Inserted.FlowerColor = 'Red' where Inserted.FlowerType = "Rose"Update Inserted set Inserted.FlowerColor = 'Purple' where Inserted.FlowerType = "Tulip"Did in a rush so be cautious about my syntax.But I think in this type of a situation its good to do at the time of creation of the SQL statement to insert the record, rather than using a trigger.U can do it in the client side (may be in ASP / JSP ...) or can be done in a stored procedure. |
 |
|
|
henrikop
Constraint Violating Yak Guru
280 Posts |
Posted - 2005-12-14 : 12:00:29
|
| I don't want to do it client side. Because then I have to be absolutely sure that I always execute the logic. It's an open project, if someone else uses the table this other developer may forget to use the logic. If I do it on the database I never have to worry who's inserting records.Henri~~~~The envious praises me unknowingly |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2005-12-14 : 12:36:00
|
| 1. Client Side programming doesn't mean programming users need to do programming on client side on each machine.2. Its good to do it in Server Side due to some other reasons as, efficiency, easy to maintain, etc.3. Users means the end users do only use the application and they don't need to / want to know what is behind the seens.4. Server side means u can write a trigger (as u asked) but it is a double edged sward (which causes big problems if u use it without knowing all the consequences), but Stored procedure seems to be much suitable in ur case. |
 |
|
|
|
|
|