创建触发器以插入数据时出现问题。 我有2台计算机,我想为每个人的订单写一个网站,并按时间出租。 但是我在编写出租功能以将数据插入订单表时遇到了麻烦。 如果计算机的ID = 1,则在7:00 am-9:00 am、12:00到14:00订购 因此,如何防止用户在当时或过去使用触发器将其放置
我尝试编写一个触发器,但是不起作用
==========================Booking====================
id_booking |id_user |total_price |status_booking |id_cpt |start_time |end_time |time_create
3 1 NULL NULL 22 1998-01-01 01:00:00.000 1998-01-01 03:00:00.000 NULL
4 1 NULL NULL 22 1998-01-01 05:00:00.000 1998-01-01 07:00:00.000 NULL
5 2 NULL NULL 23 1998-01-01 07:00:00.000 1998-01-01 09:00:00.000 NULL
========================================================
======================Computers==========================
id_cpt code_computer id_status_cpt id_room
22 VIP01 1 1
23 VIP02 1 1
=========================================================
我的触发器
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE TRIGGER beginInsert
ON booking
for insert
AS
BEGIN
--BEGIN DECALRE---
declare @id_user int
declare @total_price int
declare @status_booking int
declare @id_cpt int
declare @start_time datetime
declare @end_time datetime
declare @time_create datetime
declare @start_time_oder datetime
declare @end_time_order datetime
declare @check_time bit
--END DECALRE---
--SET VALUE OF VARIABLE---
select @id_cpt=id_cpt, @start_time_oder=@start_time_oder, @end_time_order=@end_time_order, @status_booking=0, @check_time=0
from inserted
--GET TIME IN BOOKING TO CHECK--
select @start_time=booking.start_time, @end_time=booking.end_time from booking
--END GET TIME IN BOOKING TO CHECK--
--SET VALUE OF VARIABLE---
if (@start_time_oder <@end_time_order)
begin
print(@start_time)
print(@start_time_oder)
if (@start_time_oder < @start_time and @end_time_order <@start_time) or (@start_time_oder > @end_time and @end_time_order >@end_time)
begin
insert into booking values(@id_user, @total_price, @status_booking,@id_cpt,@start_time_oder, @end_time_order, @time_create)
end
else
rollback transaction
end
else
rollback transaction
END 开始
预期结果: 当用户选择一台计算机id = 22并进行订购时,自1998年1月1日01:00:00.000至1998年1月1日03:00:00.000和1998年1月1日05:00以来,用户无法订购此计算机: 00.000至1998-01-01 07:00:00.000
非常感谢!