I am trying to write a trigger to validate if the customer/address exist before inserting but i encounter some error. Able to advise on this?
Below is the table:
Customer (CustID,CustName,DOB)
CREATE OR REPLACE TRIGGER CREATEACCOUNT
BEFORE INSERT ON ACCOUNT
FOR EACH ROW
DECLARE
newCustID varchar(10);
newPostCode int;
newStreet char;
newAccType varchar(15);
newAccStatus char(9);
newAccBalance int;
varRowCount int;
BEGIN
newCustID := :new.CustID;
SELECT COUNT(*)
INTO varRowCount
FROM Customer
WHERE CustID = newCustID;
IF (varRowCount > 0) THEN
RETURN;
END IF;
IF (varRowCount = 0) THEN
BEGIN
INSERT INTO CUSTOMER VALUES (newCustID,:new.CustName,:new.DOB);
END;
END IF;
END;
Below is the error message:
Error(27,46): PLS-00049: bad bind variable ‘NEW.CUSTNAME’
Error(27,60): PLS-00049: bad bind variable ‘NEW.DOB’
The trigger is defined on the
ACCOUNTtable. You’ve posted the definition of theCUSTOMERtable. Unless theACCOUNTtable has columnsCustNameandDOB, which seems highly unlikely, you can’t refer to:new.CustNameor:new.DOB— the:newrecord is for the row that is currently being inserted into theACCOUNTtable.Where do you intend to get the
CustNameandDOBto insert into theCustomertable?Taking a step back, why is a trigger on the
Accounttable trying to insert a row into theCustomertable in the first place. That seems like an exceptionally poor design. TheCustIDin theAccounttable would presumably be a foreign key that references theCustomertable. That would mean, though, that you could only insert the parent row in a trigger if you declare the constraints as deferable and defer them at the beginning of every transaction. The trigger would also generally have no way of determining the information for theCustomercolumns that you want to populate which is the source of the error you’re getting.