首先,建立一个视图V_tishi,统计每辆车的车牌和行驶总公里数:
再建立一个表:tishi,用来存储某个时间段的汽车车牌和行驶公里数,
然后,通过比较V_tishi和tishi中的公里数得出该不该提示的结果。
程序中用到的一些重要SQL语句:
--算差额
select tishi.car_chepai,V_tishi.sumlc-tishi.ts_gongli as chae
from V_tishi inner join tishi
on V_tishi.car_chepai=tishi.car_chepai
where V_tishi.sumlc-tishi.ts_gongli>=6000
---换机油
update tishi set tishi.ts_gongli=V_tishi.sumlc
from tishi inner join V_tishi on tishi.car_chepai=V_tishi.car_chepai
---触发器,删除相应公里数
CREATE TRIGGER tri_tishi ON [dbo].[shiyongjl]
FOR DELETE
AS
declare @chepai varchar(50),
@gongli float,
@tsgongli float
select @chepai=car_chepai,@gongli=sy_licheng from deleted
select @tsgongli=ts_gongli from tishi where car_chepai=@chepai
if @@rowcount=0
return
else
update tishi set ts_gongli=@tsgongli-@gongli
where car_chepai=@chepai