基于条件在两个表之间插​​入的函数/存储过程

时间:2013-05-12 18:30:53

标签: sql-server function

我已经很抱歉了...我对这整篇文章都很陌生,但我正在努力...

我有两张桌子......'客户'和'客房'......一个拥有独特CU_ID的顾客住在一个房间里(有一个独特的ROOM_ID,不幸的是,对我来说,有时会有2或3个CU_ID入住在同一个房间......他们通常在同一天办理登机手续,所以CheckInDate应该是相同的......

当他们办理登机手续并且在'客户'表上输入RoomNo时,我希望'Rooms'-Table中的Bit-Field'被占用'设置为“TRUE”。那部分我完成了触发器(见下文)......

诀窍是当他们结账时...如果用户手动将该房间ID的“已占用” - (位)字段标记为“FALSE”,那么我想在客户上设置DepartDate-任何住在MOMENT到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用于新的登记时,更新位列被占用为真:

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-Table,我会根据特定客户的签入日期让SQL Server进入Depart-Date。不幸的是,它仅使用Rooms-Table上针对该特定Room_ID的第一个条目更新数据......并且在两个表之间来回传递似乎很尴尬。我想我需要一个存储过程/函数来实际完成它:

  • 在插入客户记录时传递最新的CheckInDate并插入房间表字段CheckInDate
  • 当Rooms.Occupied标记为'False'时,请为Customer.RoomNo = Rooms.Room_ID设置所有CU_ID的退房日期,并将Customer.CheckInDate = Rooms.CheckInDate设置为GETDATE()......

我对第一部分感到困惑 - 如何在Insert上传递CheckInDate,如果有值,则用更新的日期更新它...

不知道,再说......我都是新人:)

提前感谢您的帮助!!!

1 个答案:

答案 0 :(得分:0)

欢迎使用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]。[客户]。[RoomNo]实际上是[RoomID]并不明显。