您好我正在制作一个SQL触发器,应该说“在 CUSTOMER_ALIAS 表中插入行时,检查该行中的三个地址列中是否包含任何一行 US_STATES 表格中存在的值(由一列 - “ COUNTRY_NAME ”组成),如果客户的地址是美国州,则设置当前正在插入'true'
的那一行的INDICIA_STATECREATE TRIGGER ADDRESS_T BEFORE INSERT ON MYDB.CUSTOMER_ALIAS
REFERENCING NEW ROW AS NEW_ROW
WHEN
EXISTS(SELECT * FROM MYDB.US_STATES WHERE US_STATES.COUNTRY_NAME = NEW_ROW.ADDRESS_1_COUNTRY OR US_STATES.COUNTRY_NAME = NEW_ROW.ADDRESS_2_COUNTRY OR US_STATES.COUNTRY_NAME = NEW_ROW.ADDRESS_3_COUNTRY)
THEN SET NEW_ROW.INDICIA_STATE= 'true'
;
我收到此错误
Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=CREATE TRIGGER ADDRESS_T BEFORE INSERT ON D;BEGIN-OF-STATEMENT;<space>, DRIVER=4.16.53
SQLState: 42601
ErrorCode: -104
根据IBM's error code list似乎是一个语法错误,但我不明白它来自哪里。这个触发器看起来怎么样?
答案 0 :(得分:0)
我认为then
不是触发器语法的一部分。您还可以使用in
简化逻辑。所以这可能有用:
CREATE TRIGGER ADDRESS_T BEFORE INSERT ON MYDB.CUSTOMER_ALIAS
REFERENCING NEW ROW AS NEW_ROW
FOR EACH ROW
WHEN EXISTS(SELECT 1 FROM MYDB.US_STATES WHERE US_STATES.COUNTRY_NAME IN (NEW_ROW.ADDRESS_1_COUNTRY, NEW_ROW.ADDRESS_2_COUNTRY, NEW_ROW.ADDRESS_3_COUNTRY)
)
SET NEW_ROW.INDICIA_STATE = 'true';
答案 1 :(得分:0)
除了戈登所说的你应该说明它是行还是声明触发器之外,我还认为WHEN子句需要它自己的一组括号:
CREATE TRIGGER ADDRESS_T
BEFORE INSERT ON MYDB.CUSTOMER_ALIAS
REFERENCING NEW ROW AS NEW_ROW
FOR EACH ROW
WHEN (
EXISTS (
SELECT * FROM MYDB.US_STATES
WHERE US_STATES.COUNTRY_NAME = NEW_ROW.ADDRESS_1_COUNTRY
OR US_STATES.COUNTRY_NAME = NEW_ROW.ADDRESS_2_COUNTRY
OR US_STATES.COUNTRY_NAME = NEW_ROW.ADDRESS_3_COUNTRY
)
)
SET NEW_ROW.INDICIA_STATE= 'true'