我已经很抱歉了...我对整个发帖内容都很陌生,但我正在尽力而为...
我有两张桌子......“客户”和“房间”......一个拥有唯一 CU_ID 的客户住在一个房间里(不幸的是,对我来说,有 2 或 3 个 CU_ID 住在同一个房间里房间...不过他们通常在同一天入住,所以 CheckInDate 应该是相同的...
当他们登记入住并在“客户”表上输入 RoomNo 时,我希望将“房间”表中的“已占用”位字段设置为“TRUE”。我用触发器完成了那部分(见下文)......
诀窍是当他们结帐时......如果用户手动将该房间 ID 的“占用”-(位)字段标记为“假”,那么我想在客户表上为任何设置 DepartDate客户在 Getdate() 的那一刻停留在那个房间。
这是我的表格、触发器和一些测试数据:
CREATE TABLE [dbo].[Rooms](
[Room_ID] [int] IDENTITY(1,1) NOT NULL,
[Room_No] [nvarchar](50) NULL,
[Occupied] [bit] NULL,
[CheckInDate] [int] NULL,
CONSTRAINT [PK_Rooms] PRIMARY KEY CLUSTERED
(
[Room_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Customer](
[CU_ID] [int] IDENTITY(5000,1) NOT NULL,
[CheckInDate] [datetime] NULL,
[RoomNo] [int] NOT NULL,
[Nights_Booked] [int] NULL,
[DepartDate] [datetime] NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
[CU_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Customer] WITH CHECK ADD CONSTRAINT [FK_Customer_Rooms] FOREIGN KEY([RoomNo])
REFERENCES [dbo].[Rooms] ([Room_ID])
GO
ALTER TABLE [dbo].[Customer] CHECK CONSTRAINT [FK_Customer_Rooms]
GO
-- 2 Tables created including PK/FK Relationship
这是我的第一步触发器......当 Room_ID 用于新的入住时,将位列 Occupied 更新为 True:
Create TRIGGER [dbo].[Occupied]
ON [dbo].[Customer]
FOR INSERT
NOT FOR REPLICATION
AS
BEGIN
IF TRIGGER_NESTLEVEL() > 1
RETURN
UPDATE Rooms
SET [Occupied] = 'True'
FROM Rooms r
JOIN Customer cu
ON cu.[RoomNo] = r.[Room_ID]
Join INSERTED INS
ON cu.[RoomNo] = INS.[RoomNo]
END
GO
我在他们两个中都输入了一些测试数据......
SET IDENTITY_INSERT Rooms ON
INSERT INTO Rooms
(Room_ID, Room_No, Occupied)
SELECT 1, 'A14', 0 UNION ALL
SELECT 2, 'B2', 0 UNION ALL
SELECT 3, 'C3', 0 UNION ALL
SELECT 4, 'D8', 0 UNION ALL
SELECT 5, 'K9', 0
SET IDENTITY_INSERT Rooms OFF
GO
SET IDENTITY_INSERT Customer ON
INSERT INTO Customer
(CU_ID, CheckInDate, RoomNo, Nights_Booked, DepartDate)
SELECT 5000, '2013-05-10', 1, 4, NULL UNION ALL
SELECT 5001, '2013-05-10', 1, 4, NULL UNION ALL
SELECT 5002, '2013-05-10', 2, 2, NULL UNION ALL
SELECT 5003, '2013-05-10', 3, 3, NULL UNION ALL
SELECT 5004, '2013-05-11', 4, 4, NULL UNION ALL
SELECT 5005, '2013-05-11', 4, 4, NULL UNION ALL
SELECT 5006, '2013-05-11', 4, 4, NULL
SET IDENTITY_INSERT Customer OFF
-- Test Data entered in rows on 'Rooms' and 'Customer'-Tables
触发器工作正常,它更新所有具有相同 Room_ID 的记录(分别在客户表上的 RoomNo)。
我试图用其他触发器解决我的问题。如果我将特定客户的入住日期传递给房间表,我会让 SQL Server 根据特定客户的入住日期输入出发日期。不幸的是,它只使用为房间表上的特定 Room_ID 制作的第一个条目更新数据......而且在两张表之间来回传递似乎很尴尬。我想我需要一个存储过程/函数来真正实现这一点:
- 在插入客户记录时通过 NEWEST CheckInDate 并插入到 Room-Table 字段 CheckInDate
- 当 Rooms.Occupied 标记为“False”时,将所有 CU_ID 的退房日期设置为 Customer.RoomNo = Rooms.Room_ID AND Customer.CheckInDate = Rooms.CheckInDate 为 GETDATE() ...
我在第一部分苦苦挣扎-如何在插入时传递 CheckInDate 以及如果存在值,则使用较新的日期对其进行更新...
不知道,再次......我是新人:)
感谢您提前提供任何帮助!