Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a default value for one column in SQL based on another column

Tags:

sql

I'm working with an old SQL 2000 database and I don't have a whole lot of SQL experience under my belt. When a new row is added to one of my tables I need to assign a default time value based off of a column for work category.

For example, work category A would assign a time value of 1 hour, category B would be 2 hours, etc...

It should only set the value if the user does not manually enter the time it took them to do the work. I thought about doing this with a default constraint but I don't think that will work if the default value has a dependency.

What would be the best way to do this?

like image 821
Dynamo Avatar asked Apr 02 '09 19:04

Dynamo


1 Answers

I would use a trigger on Insert.

Just check to see if a value has been assigned, and if not, go grab the correct one and use it.

like image 67
Stephen Wrighton Avatar answered Sep 21 '22 23:09

Stephen Wrighton