基于不同列的值的默认列值 [英] Default column value based on the value of a different column

查看:32
本文介绍了基于不同列的值的默认列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQL Server 2005.我有一个带有 ColumnA 位、ColumnB 整数的表

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

我可以为 ColumnB 添加一个默认值,以便 ColumnB 如果 ColumnA 为 1 且 ColumnB 为 15如果 ColumnA 为 0,则为 0?

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).

推荐答案

如果您的 ColumnB 只能是 15 或 0,您可以将其设为基于 ColumnA 的计算列.这是添加新计算列的代码:

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

这篇关于基于不同列的值的默认列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆