我需要在表 Major
中添加一个列 IsDeleted
,默认值为:
当 MajorName 像 '%Engineering%'
时为真,而在其他没有单词 Engineering 的时候为假
最佳答案
将其添加为 computed column (又名虚拟列)
alter table Major add IsDeleted as
cast (case when MajorName like '%Engineering%' then 1 else 0 end as bit)
演示
create table Major (MajorName varchar (100))
alter table Major add IsDeleted as
cast (case when MajorName like '%Engineering%' then 1 else 0 end as bit)
insert into Major (MajorName) values
('This is the Engineering department'),('Hello world')
select * from Major
+------------------------------------+-----------+
| MajorName | IsDeleted |
+------------------------------------+-----------+
| This is the Engineering department | 1 |
+------------------------------------+-----------+
| Hello world | 0 |
+------------------------------------+-----------+
关于sql - 将列添加到 SQL Server 数据库中的表并使用案例设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40951891/