如何在sql中激活描述?

时间:2017-10-14 04:45:40

标签: oracle11g triggers

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

1 个答案:

答案 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