日期:2014-05-17 浏览次数:20416 次
create table tb(d_xiaoshi numeric(18,0),d_fenzhong numeric(18,0),d_qiandao varchar(10))
go
create trigger tb_qiandao
on tb
INSTEAD OF insert
as
begin
insert into tb select d_xiaoshi,d_fenzhong,right('0'+convert(varchar,convert(int,d_xiaoshi)),2)+':'+right('0'+convert(varchar,convert(int,d_fenzhong)),2) from inserted
end
go
insert into tb(d_xiaoshi,d_fenzhong)select 1,2
go
select * from tb
/*
d_xiaoshi d_fenzhong d_qiandao
--------------------------------------- --------------------------------------- ----------
1 2 01:02
(1 行受影响)
*/
go
drop table tb
--方法1 触发器
create table fan1
(d_xiaoshi numeric,
d_fenzhong numeric,
d_qiandao varchar(100))
create trigger tr_fan1
on fan1 instead of insert
as
begin
insert into fan1
select d_xiaoshi,d_fenzhong,
case len(d_xiaoshi) when 1 then '0'+rtrim(cast(d_xiaoshi as int))
else rtrim(cast(d_xiaoshi as int)) end + ':' +
case len(d_fenzhong) when 1 then '0'+rtrim(cast(d_fenzhong as int))
else rtrim(cast(d_fenzhong as int)) end
from inserted
end
insert into fan1(d_xiaoshi,d_fenzhong) values(1,2)
select * from fan1
/*
d_xiaoshi d_fenzhong d_qiandao