翻译:
1、就是假定更新PUBS数据库中的TITLES表的price字段,可以增长不超过10%,不允许降低价格,也不允许修改主键title_id,要求:
a.写个after trigger来判断这个操作正不正确
b.写个instead of trigger来完成这个操作
1、就是假定更新PUBS数据库中的TITLES表的price字段,可以增长不超过10%,不允许降低价格,也不允许修改主键title_id,要求:
a.写个after trigger来判断这个操作正不正确
b.写个instead of trigger来完成这个操作
a.创建个自定义函数计算停车收费
公式如下:总费用=(出时间-进时间)* 3/每小时
b.创建包括字段TimeIn,TimeOut和Total Fee的表。
Total Fee用Money类型,下面这句不太明白,是不是显示如何应用上面创建的自定义函数?
不过不知道,如果1小时10分钟算1小时还是两小时
结果是1最少我的SERVER是这样的,难道你的是2?
create function f_calc(
@进时间 datetime,
@出时间 datetime
)returns money
as
begin
return(datediff(minute,@进时间,@出时间)/cast(60 as money)*3)
end
go--b.创建包括字段TimeIn,TimeOut和Total Fee的表。Total Fee用Money类型
create table 表名(TimeIn datetime,TimeOut datetime,Total_Fee as dbo.f_calc(TimeIn,TimeOut))
,datediff(hour,'10:00','11:59')
--结果:
----------- -----------
2 1(所影响的行数为 1 行)
create trigger tr_update on titles
after update
as
if exists(
select 1 from inserted n join deleted o on n.title_id=o.title_id
where n.price<o.price or (n.price-o.price)/o.price>0.1
)
print '操作不正确' --当然这种提示是不会提示给前台的,如果要提示给前台,得用raiserror('操作不正确',16,1), 下面的相同
else
print '操作正确'
create trigger tr_update on titles
instead of update
as
if exists(
select 1 from inserted n join deleted o on n.title_id=o.title_id
where n.price<o.price or (n.price-o.price)/o.price>0.1
)
print '操作不正确' --当然这种提示是不会提示给前台的,如果要提示给前台,得用raiserror('操作不正确',16,1), 下面的相同
else
update titles set price=n.price
from titles a join inserted n on n.title_id=o.title_id
create function f_calc(
@进时间 datetime,
@出时间 datetime
)returns money
as
begin
return((datediff(minute,@进时间,@出时间)+59)/60*3)
end
go