首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一个简单的触发器的有关问题

2012-01-30 
一个简单的触发器的问题哥们没写过这东西想实现这样的功能就是一个表中有4个字段都是bit类型默认值都为0想

一个简单的触发器的问题
哥们没写过这东西
想实现这样的功能   就是   一个表中   有   4个字段   都是bit   类型   默认值   都为0   想让其中三个字段(固定的三个字段)为1时   另外一个自动变成   1   请问如何来实现   谢谢

[解决办法]
create trigger tr_表 on 表
for insert,update
as
if exists(select 1 from inserted where 字段1=1 and 字段2=1 and 字段3=1)
begin
update 表
set 字段4=1
from 表, inserted i
where 表.字段4=0 and i.字段1=1 and i.字段2=1 and i.字段3=1
end
[解决办法]
create trigger tr_tablename_update
on tablename
for update,insert
as

update a
set 另外一个=1
from tablename a,inserted i
where a.关键字=i.关键字
and a.固定1=1
and a.固定2=1
and a.固定3=1
and a.另外一个=0

go

[解决办法]
关键字指的你实际的主键,比如ID列.
我上面写的那个有问题,没有写关联条件.

热点排行