日期:2014-05-16 浏览次数:20479 次
create table O_ne(Id int, Name varchar(10))
insert into O_ne
select 1 ,'上海' union all
select 2 ,'北京' union all
select 3 ,'重庆'
--1.主键
create table T_ow(
Fordate datetime,
Id Int,
Fqlp int,
primary key(Fordate,Id) --主键
)
insert into T_ow
select '2001-01-01', 1 ,120 union all
select '2001-01-02', 2 ,135 union all
select '2001-01-03', 3 ,20 union all
select '2013-12-31', 31 ,110
go
--2.查询
select t.Id,o.Name,t.Fordate,t.Fqlp
from T_ow t
inner join O_ne o
on t.Id = o.Id
where t.Fordate >='2001-01-01' and t.Fordate <= '2001-12-31'
and t.Fqlp <> 0
order by Fordate
/*
Id Name Fordate Fqlp
1 上海 2001-01-01 00:00:00.000 120
2 北京 2001-01-02 00:00:00.000 135
3 重庆 2001-01-03 00:00:00.000 20
*/
--3.创建触发器
create trigger dbo.trigger_T_ow_insert
on T_ow
after insert,update
as
if exists(select 1 from inserted where Fqlp = 0)
rollback
go
--4.在插入时fqlp 为0,所以报错
insert into T_ow
values('2001-01-08', 3 ,0)
/*
消息 3609,级别 16,状态 1,第 1 行
事务在触发器中结束。批处理已中止。
*/
--fqlp不为0,所以不报错
insert into T_ow
values('2001-01-08', 3 ,100)