sql 触发器例子写法,急求。
我有一张表,表里的数据时是实时数据库插进去的。现在有几个字段是时间字段它们的取值为1900 00:00:00,我想让插入数据时此字段的时间变为null,怎样写触发器???
[解决办法]
你不用写触发器,在插入时,将该字段值设置为null即可。
例如:
insert into tb values( 1 , 2, null , 3 , null)
[解决办法]
如果已经插入了,直接更改之。
update tb set col = null where convert(varchar(10),col,120) = '1900-01-01'
[解决办法]
如果你一定要用触发器,可如下:CREATE TRIGGER insertOfTimeOn$_SILO_STATES ON dps.SILO_STATES for INSERTAS BEGIN SET NOCOUNT ON update dps.SILO_STATES set IN_START_TIME = (case when i.IN_START_TIME = '1900-01-01 12:00:00.000' then null else IN_START_TIME end), IN_END_TIME = (case when i.IN_END_TIME = '1900-01-01 12:00:00.000' then null else IN_END_TIME end) from dps.SILO_STATES m , inserted i where m.关键字 = i.关键字END
[解决办法]
create trigger tr_SILO_STATESon SILO_STATES after insertasbegin if exists(select 1 from inserted where IN_START_TIME='1900-01-01 12:00:00.000') begin update a set a.IN_START_TIME=null from SILO_STATES a inner join inserted b on a.[主键字段]=b.[主键字段] and b.IN_START_TIME='1900-01-01 12:00:00.000' end end
[解决办法]