我创建了三个表。
CREATE TABLE Clients
(
ClientID INT IDENTITY(1,1) PRIMARY KEY,
First_Name VARCHAR(50) NOT NULL,
Last_Name VARCHAR(50) NOT NULL,
)
CREATE TABLE Reservation
(
ReservationID INT IDENTITY(1,1) PRIMARY KEY,
ClientID INT FOREIGN KEY (ClientID) REFERENCES Clients(ClientID),
Reservation_paid VARCHAR(3) DEFAULT 'NO',
)
CREATE TABLE Payment
(
Payment_ID INT IDENTITY(1,1) PRIMARY KEY,
ClientID INT FOREIGN KEY (ClientID) REFERENCES Clients(ClientID),
ReservationID INT FOREIGN KEY (ReservationID) REFERENCES Reservation(ReservationID),
)
每当客户确实支付预订时,我想在Reservation表中将Reservation_paid列的值更改为YES,我想用触发器自动执行。
示例:如果Reservation表中的ClientID
自动存在于Payment表中,则Reservation_paid的值将设置为YES。
提前谢谢。
答案 0 :(得分:1)
tm
答案 1 :(得分:0)
插入后触发器应该做这样的事情
UPDATE R
SET Reservation_paid = 'Yes'
FROM reservation R
WHERE EXISTS (SELECT 1
FROM INSERTED I
WHERE I.clientid = R.clientid
AND I.reservationid = R.reservationid)
答案 2 :(得分:0)
在表Reservation
的{{1}}列的insert
或update
之后,编写一个可在表ClientId
上使用的触发器。然后将Payment
与ClientID
表的ClientID
列进行匹配,并将相应的Reservation
更新为YES。
编辑: 触发器将是这样的
Reservation_paid
答案 3 :(得分:0)
CREATE TRIGGER trgAfterInsert ON [dbo].[Payment]
FOR INSERT
AS
declare @ClientID int;
select @ClientID =i.ClientID from inserted i;
insert into Reservation(ClientID,Reservation_paid)
values(@ClientID,'Yes');
--PRINT 'AFTER INSERT trigger fired.'
GO