CREATE OR REPLACE TRIGGER R_SERVICE_HISTORY_MESSAGE
AFTER INSERT ON SERVICE_HISTORY_DETAIL
FOR EACH ROW
AS IF(SELECT FINISHED FROM SERVICE_HISTORY, INSERTED
WHERE FINISHED=’T’
BEGIN
PRINT ‘Hi <Full Customer Name>, your dog <dog-name> of breed: <breed-name> is ready for pick-up at Doggo Paradise Carindale’
END;
ELSE
IF(SELECT FINISHED FROM SERVICE_HISTORY, INSERTED
WHERE FINISHED=’F’
BEGIN
PRINT ‘Hi <Full Customer Name>, your dog <dog-name> of breed: <breed-name> is not ready to be picked up yet.’
END;
/
enter image description here 表结构如下,我的触发器在SERVICE_HISTORY_DETAIL中插入或更新后激活是完全错误的,以及如何用其他表中的名称填充描述,我已经尝试了很多次,我真的很困惑。欢呼声
SERVICE_HISTORY DOGS CUSTOMERS
----------------------------------------------------------------------
MESSAGE varchar(300) DOG_ID C_ID
DOG_ID
FINISHED C_ID NAME
答案 0 :(得分:0)
这是一个经过测试的工作示例,您可以展开
CREATE TRIGGER R_SERVICE_HISTORY_MESSAGE
ON SERVICE_HISTORY
AFTER INSERT,update
AS
declare @cname varchar(100)
set @cname = (select concat(title,' ',firstname,' ',lastname) from dimcustomer join inserted on customerkey = inserted.cid)
IF exists(SELECT s.FINISHED FROM SERVICE_HISTORY s join INSERTED i on i.dog_id = s.dog_id WHERE s.FINISHED='T')
BEGIN
PRINT concat('Hi ',
@cname,
' your dog <dog-name> of breed: <breed-name> is ready for pick-up at Doggo Paradise Carindale')
END
ELSE
IF exists(SELECT s.FINISHED FROM SERVICE_HISTORY s join INSERTED i on i.dog_id = s.dog_id WHERE s.FINISHED='F')
BEGIN
PRINT 'Hi <Full Customer Name>, your dog <dog-name> of breed: <breed-name> is not ready to be picked up yet.'
end