Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default column value based on the value of a different column

SQL Server 2005. I have a table with ColumnA bit, ColumnB int

Can I add a default value to ColumnB so that ColumnB is 15 if ColumnA is 1 and ColumnB is 0 if ColumnA is 0?

I know I could do this with a trigger but I my boss is prejudice against triggers (he needs trigger sensitivity training).

like image 849
codingguy3000 Avatar asked Dec 07 '22 04:12

codingguy3000


2 Answers

if your ColumnB can only be 15 or zero, you can make it a computed column based on ColumnA. here is the code to add a new computed column:

ALTER TABLE YourTable ADD YourComputedColumn AS ColumnA*15
go
like image 135
KM. Avatar answered Jan 06 '23 07:01

KM.


Have you considered a computed column instead?. It's not exactly the same thing... but perhaps it is enough for you?

like image 38
Mark Byers Avatar answered Jan 06 '23 07:01

Mark Byers