根据条件sql-server添加值

时间:2014-10-16 09:17:43

标签: sql-server triggers

我有一张这样的表:

create table example(
id char(1),
color varchar,
col_binary binary);

我已经有了一个存储过程,我希望通过存储过程返回一个颜色,例如" blue"然后col_binary列取值1,并使用另一种颜色,例如"红色" col_binary取值为0。 我想我需要一个触发器。

1 个答案:

答案 0 :(得分:0)

你可以通过添加像这样的触发器来实现它

Create Trigger [Add_LTD] on Example
After Insert As
BEGIN
Update Example set Example.[col_binary]=case when Example.[Color]='Red' then 1 else 0 END from Inserted 
where Example.id=Inserted.id
END;

DEMO