我已经很抱歉...我对整个发布工作还是很陌生的,但是我正在竭尽全力...
我有两个表...“客户”和“房间” ...一个具有唯一CU_ID的客户住在一个房间(带有一个唯一ROOM_ID,对我来说很不幸,有时有两个或三个CU_ID留在同一房间房间...虽然他们通常在同一天入住,所以CheckInDate应该相同...
当他们签入并在“客户”表上输入RoomNo时,我希望将“客房”表中的“已占用”位字段设置为“ TRUE”。我用扳机完成了这一部分(见下文)...
诀窍是当他们签出时...如果用户手动将该Room-ID的“已占用”-(位)字段标记为“假”,那么我想在客户表上将ANY设置为DepartDate客户在MOATE到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用于新签到时,将位列更新为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(分别在Customer Table上的RoomNo)更新所有记录。
我试图用其他触发器来解决我的问题。如果我将SQL Server传递给Room-Table,我将根据特定客户的签到日期输入SQL Server的输入日期。不幸的是,它仅使用为Rooms-Table上的特定Room_ID创建的第一个条目来更新数据...,在两个表之间来回传递似乎很尴尬。我想我需要一个存储过程/函数才能实际完成该任务:
在插入客户记录时,传递NEWEST CheckInDate并将其插入房间表字段CheckInDate
当Rooms.Occupied标记为“ False”时,将所有CU_ID的Check-Out-Date设置为Customer.RoomNo = Rooms.Room_ID AND Customer.CheckInDate = Rooms.CheckInDate到GETDATE()...
我在第一部分中苦苦挣扎-如何在插入时传递CheckInDate,如果值存在,则用较新的日期对其进行更新...
不知道,再次...我是新来的:)
谢谢您的任何帮助!
最佳答案
欢迎使用StackOverflow。将来,如果您将这些示例简化,将对我们非常有帮助。例如,在这种情况下,我们所需要做的就是每个表中的几列(没有任何选项/约束)。
您不应该手动更新位。请改用存储过程。
CREATE PROCEDURE [dbo].[usp_CheckoutRoom] (
@RoomID
)
AS
UPDATE [dbo].[Rooms]
SET [Occupied] = 0
WHERE [Room_ID] = @RoomID
UPDATE [dbo].[Customer]
SET [DepartDate] = GETDATE()
WHERE [RoomNo] = @RoomID
END
我假设您仍在进行系统设计,但是如果单个客户签出多个房间怎么办?您需要一个客户/房间交叉引用表。
[dbo].[Customer]
[dbo].[Room]
[dbo].[CustomerRoom]
还要注意,[dbo]。[Customer]。[RoomNo]实际上不是[RoomID],这一点并不明显。
关于sql-server - 根据条件在两个表之间插入的函数/存储过程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16510695/