Oracle SQL - ORA-04079: invalid trigger specification - sql

I have an EMPLOYEE table, which I want to create a trigger to log for when the employee commissions change (EMPCOMM). I have created an EMPLOYEE_COMM_AUDIT table to handle this. I have come up with the following code:
CREATE OR REPLACE TRIGGER EMPLOYEE_COMM_AUDIT_TRIGGER
BEFORE DELETE OR INSERT OR UPDATE OF EMP_COMM ON EMPLOYEE
IF (NEW.EMP_COMM != OLD.EMPCOMM)
BEGIN
UPDATE EMPLOYEE_COMM_AUDIT
SET EMPLOYEE_COMM_AUDIT.EMP_NUM = EMPLOYEE.EMP_NUM;
SET EMPLOYEE_COMM_AUDIT.CHANGE_DATE = (SYSDATE, 'MM-DD-YYYY HH24:MI:SS') FROM DUAL;
SET EMPLOYEE_COMM_AUDIT.USERNAME = CURRENT_USER;
SET EMPLOYEE_COMM_AUDIT.ACTION = NULL;
SET EMPLOYEE_COMM_AUDIT.OLD_COMM = OLD.EMPCOMM;
SET EMPLOYEE_COMM_ADUDIT.NEW_COMM = NEW.COMM;
DBMS_OUTPUT_LINE("Employee Commisions Audit has been updated);
END;
However Oracle SQL tells me: ORA-04079: invalid trigger specification, but I'm not getting any red underlines anywhere to indicate where the fault is.
Can somebody please help me out? I have tried to have a look on these forums, but I can't seem to find a solid reply anywhere.
Thanks in advance.

Your UPDATE syntax is all wrong:
UPDATE EMPLOYEE_COMM_AUDIT
SET CHANGE_DATE = SYSDATE,
USERNAME = CURRENT_USER,
ACTION = NULL,
OLD_COMM = OLD.EMPCOMM,
NEW_COMM = NEW.COMM
WHERE EMP_NUM = :NEW.EMPNUM;
Changes and assumptions:
SET only appears once.
The table name does not need to be repeated.
The SET expressions are separated by commmas not semi-colons.
There is no need to convert sysdate to a date to assign it to a date column.
You need a WHERE clause to specify what row should be updated.
This fixes the update (or attempts to); there may be other issues.

Related

SQL TRIGGER ON UPDATE

Hey guys i am trying to make a trigger to update the feild Bill.BillAmmount everytime a new row is added to Reading
The feild should = (Reading.MeterReading - Reading.LastMeterReading)*Resedent.Rate )
REDEDENT
BILL
READING
are the 3 tables
also thankyou sooo much
AFTER INSERT ON Reading
Begin
UPDATE bill
SET bill.BillAmount = (SELECT (Reading.MeterReading - Reading.LastMeterReading)*Resedent.Rate
FROM Reading, Resedent
WHERE Reading.ReadingID = ReadingID AND Resedent.AccountID = AccountID)
End;
DROP TRIGGER BILLAMOUNT_ON_INSERT; ```
In Oracle, a trigger should be referring to :NEW (or perhaps :OLD). In addition, Oracle doesn't support FROM in UPDATE statements.
I suspect you want something like this:
Begin
UPDATE bill b
SET BillAmount = (SELECT (:new.MeterReading - :new.LastMeterReading) * rs.Rate
FROM Resedent rs
WHERE rs.ReadingID = :new.AccountID
)
WHERE b.AccountId = :new.AccountId
End;
However, without more information about your data model, it is hard to be specific about what you really want to do.

how to do an update if possible , otherwise do an insert in oracle

Is it possible to raise an exception when an update statement doesn't work.
eg:
UPDATE tableA
SET x = 2,
modified_by = userID,
WHERE prod_id = in_prod_id
AND loc_id = in_loc_id
AND invType = in_InvType
AND inv_status = 'N'
But if that statement didn't find the row it needs to update I want to be able to raise an exception or somehow identify it so I can perform an insert statement.
I tried doing
EXCEPTION
WHEN NO_DATA_FOUND
THEN
-- insert into.....
, but that didn't work.. cuz it never raises the exception as i thought it would.
One solution I found was to do a check and see if such a record exists in the table to perform the update.. But I want to know if there is a better way of doing it.
Thank you.
what you need is a merge statement
See if this can help you : http://www.oratable.com/oracle-merge-command-for-upsert/

Update field in linked table in firebird

I have two tables - card and journal. The card's id (code) is a linker in journal(card-code)
.One card can have many journals and I need to update a field(time) in this card table from journal with trigger - I have wrote the current trigger but it has errors.Please help me to locate it.
AS
DECLARE VARIABLE currentTimeOfChanging timestamp;
begin
select current_timestamp from rdb$database into currentTimeOfChanging;
update card
set card.lastupdate = currentTimeOfChanging;//!error
where card.code = journal.cardcode
end
I think you want trigger like this:
CREATE TRIGGER SetLastUpdateTS FOR journal
ACTIVE AFTER UPDATE
AS
BEGIN
UPDATE card SET lastupdate = CURRENT_TIMESTAMP WHERE code = NEW.cardcode;
END
Some comments:
you don't need the currentTimeOfChanging temp variable, you can use the current_timestamp directly in the UPDATE statement;
you have to prefix the variables with ":" and there is no ";" after the variable in the UPDATE statement in the PSQL code. So your original statement should have been
update card set card.lastupdate = :currentTimeOfChanging where ...
instead of journal.cardcode you use NEW and/or OLD context variables to refer the values of the current statement (which triggered the trigger).

SQl trigger after insert issue

I am working on triggers using SQL server 2005. I have a trigger for a table which is after update. After declaring the variables, the code is like this.
if #isconfirmed_before = 0 and #isconfirmed_after = 1
begin
if #invite_userid <> ''
begin
select #points = points from dbo.InvitePoint where code = 'USR' and packageid = #packageid
INSERT INTO InviteCount
([userID]
,[joinMerchantID]
,[packageID]
,[points]
,[joinDate])
VALUES
(#invite_userid
,#merchantid
,#packageid
,#points
,getdate())
end
SET #alpha_numeric=''
SELECT #alpha_numeric=#alpha_numeric+CHAR(n) FROM
(
SELECT TOP 8 number AS n FROM master..spt_values
WHERE TYPE='p' and (number between 48 and 57 or number between 65 and 90)
ORDER BY NEWID()
) AS t
update merchant
set reg_code = #alpha_numeric
where merchantid = #merchantid
END
The last part of
update merchant
set reg_code = #alpha_numeric
where merchantid = #merchantid
This reg_code shoule be inserted only once when the row is inserted but it is changing every time there is an update to the table. How do i make it happen. Please help me, Thank you in advance!!
update merchant
set reg_code = #alpha_numeric
where merchantid = #merchantid
That code is executing every time there is an update, because you have no conditional flow preventing it from executing sometimes. If you want to only execute that given a certain condition, you'll need to wrap it in an IF block.
You say: "This reg_code shoule be inserted only once when the row is inserted but it is changing every time there is an update to the table." Why is that in an AFTER UPDATE trigger then? It looks like it should be in an AFTER INSERT trigger. Unless I am misunderstanding you.
OK you are very much in trouble with triggers. First you need to show the actual create trigger code. I suspect you don't have it set correctly to only fire on insert.
Next you need to assume that the trigger must be able to handle multiple record inserts. Anytime you are setting a value to a scalar variable in a trigger, you are probably doing something wrong. This can't be your whole trigger either because you haven't shown how you get the varible values.

Update more than one row in oracle

I want to update the table values by using hard coded values.here is my code :
BEGIN
UPDATE emp_table
SET expiry_dt = TO_DATE('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS'),
WHERE emp_id = '78629160';
UPDATE emp_table
SET expiry_dt = TO_DATE('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS'),
WHERE emp_id = '78629160';
END
I want to do it in the single update statement. Can anyone tell me the solution?
UPDATE emp_table SET expiry_dt = TO_DATE
('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS'),
WHERE emp_id IN ('78629160','111020102','88888888');
Should do you. Edited the IN clause with various employee id's as yours were identical.
UPDATE emp_table
SET expiry_dt =
CASE
WHEN emp_id = '78629160' THEN TO_DATE('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS')
WHEN emp_id = '78629161' THEN TO_DATE('21.10.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS')
END
WHERE emp_id IN ('78629160', '78629161')
I assume the fact that you have the same ID twice was just a copy and paste error, just like the fact that both dates are identical.
Btw: what data type is emp_id? If that is a numeric type, get rid of the single quotes for the literals (numeric literals should not be quoted). They will prevent the usage of an index on that column!
Begin UPDATE emp_table SET expiry_dt = TO_DATE ('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS'),-- why you are using ',' here??
WHERE emp_id = '78629160';
Begin
UPDATE emp_table SET expiry_dt = TO_DATE('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS')
WHERE emp_id = '78629160';
end;
It is running in my machine whithout any error and problem.. adn giving expected result also..
try once again without using that ','.