I have 3 triggers in a .sql file.
CREATE TRIGGER NEW_HIRED
AFTER INSERT ON EMPLOYEE
FOR EACH ROW
UPDATE COMPANY_STATS SET NBEMP = NBEMP + 1;
CREATE TRIGGER FORMER_EMP
AFTER DELETE ON EMPLOYEE
FOR EACH ROW
UPDATE COMPANY_STATS SET NBEMP = NBEMP - 1;
CREATE TRIGGER FORMER_EMP1
AFTER DELETE ON EMPLOYEE
FOR EACH ROW
UPDATE COMPANY_STATS SET NBEMP = NBEMP - 1;
When I run those, it gives me below error:
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token "END-OF-STATEMENT" was found following "
NBEMP". Expected tokens may include: "". LINE
NUMBER=1. SQLSTATE=42601
How can I fix this? Thanks in advance.!
Related
I'm trying to convert the below trigger which is written Oracle to PostgreSQL:
create or replace TRIGGER "SCHEMA1".table1_ta
after delete
on table1
for each row
begin
if :old.hid = 0 or :old.hid = -1
then
err.raise(-12345, 'Error Message');
end if;
end table1_ta;
Below is the code which is getting generated as part of the schema conversion process of the AWS Schema Conversion Tool (SCT) but getting an error when I'm trying to apply it to the target PostgreSQL database.
CREATE TRIGGER table1_ta
AFTER DELETE
ON schema1.table1
FOR EACH ROW
EXECUTE PROCEDURE schema1.table1_ta$table();
Below is the error that I'm getting when AWS SCT is trying to create this trigger:
ERROR: function schema1.table1_ta$table(); does not exists.
How can I fix this?
I have three tables below:
I need to create a trigger to disallow students to take a class without completing their pre-requisites (must use a trigger). The trigger must return an error message "Missing Pre-req" when trying to insert a class without its proper pre-req.
So far, I wrote a command below:
create trigger checkPrereq
before insert on HW3.SCHEDULE
referencing new as newRow
for each row
When
(
select GRADE
from HW3.SCHEDULE
where HW3.SCHEDULE.ClassId =
(
select PrereqId from HW3.CLASS_PREREQ
where newRow.ClassId = HW3.CLASS_PREREQ.ClassId
)
) in ('F', null)
begin atomic
set newRow.Semester = null;
signal sqlstate 'KILLED' set message_text = ('Missing Pre-req');
end
but I got a warning:
DB21034E The command was processed as an SQL statement because it was
not a valid Command Line Processor command. During SQL processing it
returned: SQL0104N An unexpected token "GRADE" was found following
"ach row When (select". Expected tokens may include: ")". LINE
NUMBER=1. SQLSTATE=42601
I can not understand what was happening here. Could you please help fix this? Any help is appreciated!
I have multiple Insert Statementes:
INSERT INTO table1(item_name) VALUES ('item1');
INSERT INTO table1(item_name) VALUES ('item2');
INSERT INTO table1(item_name) VALUES ('item3');
the first value already exists in the table. So i get an error: ERROR: duplicate key value violates unique constraint when the first statement is executed and the execution of the other statements stops.
How can i force Postgres to execute the other statements despite the error? I know that INSERT ON CONFLICT in this case would be a solution, my question is more general: How to continue with the following statements if an error occurs.
If you send several SQL statements, separated by semicolon, to the PostgreSQL server in a single query, they are executed in a single transaction. So if one of them fails, all are undone and processing terminates.
You have to execute each statement separately for that.
If you use psql CLI instead of pgadmin, you can use ON_ERROR_ROLLBACK setting.
When set to on, if a statement in a transaction block generates an
error, the error is ignored and the transaction continues. When set to
interactive, such errors are only ignored in interactive sessions, and
not when reading script files. When set to off (the default), a
statement in a transaction block that generates an error aborts the
entire transaction. The error rollback mode works by issuing an
implicit SAVEPOINT for you, just before each command that is in a
transaction block, and then rolling back to the savepoint if the
command fails.
Example:
select * from t;
x
---
1
(1 row)
\set ON_ERROR_ROLLBACK on
begin;
BEGIN
insert into t values(1);
ERROR: duplicate key value violates unique constraint "t_pkey"
DETAIL: Key (x)=(1) already exists.
insert into t values(2);
INSERT 0 1
insert into t values(3);
INSERT 0 1
commit;
COMMIT
select * from t;
x
---
1
2
3
(3 rows)
Can anyone see why I'm getting the "No current record for fetch operation" below?
I'm successfully skipping duplicate records by catching and not re-throwing the unique key violation exception below. There is another error however.
FOR SELECT
...
FROM
P_SELECT_CLAIM_FILE cf
ORDER BY
cf.DATESBM, cf.TIMESBM, cf.TRANSCDE
INTO
...variables
DO BEGIN
INSERT INTO
CLAIM_TABLE (...)
VALUES (...variables);
WHEN GDSCODE unique_key_violation DO
TX_ID=0;
WHEN GDSCODE no_cur_rec DO
/* Why does this happen?
-508 335544348 no_cur_rec No current record for fetch operation
*/
EXCEPTION E 'no_cur_rec ' || TX_ID;
END
The procedure P_SELECT_CLAIM_FILE contains another FOR SELECT INTO with lots of trimming and finally a SUSPEND command. This reads from a fixed width text file.
I'm tempted to change this into a single INSERT SELECT where not exists statement. I prefer to make a minimal fix instead however; the holidays already here.
CREATE TRIGGER TRG_EFMREFNO
BEFORE
INSERT ON FEEDBACK_CASE_TB
FOR EACH ROW
BEGIN
SELECT SEQ_EFMREFNO.NEXTVAL INTO:NEW.EFMREFNO FROM DUAL;
END;
please help me it's give errors
An unexpected token "CREATE TRIGGER TRG_EFMREFNO
BEFOR" was found following "BEGIN-OF-STATEMENT". Expected tokens may include: "<revoke>".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.12.79
An unexpected token "END-OF-STATEMENT" was found following "END". Expected tokens may include: "JOIN <joined_table>".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.12.79
Please give the solution for that errors
You have 2 things going on here -
1) When the ";" character is part of the SQL statement, it's necessary to use a different character to terminate the statement. I typically use "#". To tell the "db2" command that you have chosen a different character, use
db2 -td#
or if you want to read from a file
db2 -td# -f <somefile>
2) The correct way to update new row in a trigger is to set an alias for the new row, and use a set clause:
CREATE TRIGGER TRG_EFMREFNO
BEFORE
INSERT ON FEEDBACK_CASE_TB
REFERENCING NEW AS N
FOR EACH ROW
BEGIN
SET N.EFMREFNO = SEQ_EFMREFNO.NEXTVAL;
END
#
There may be other ways to use the sequence with the default clause in the create table statement that will accomplish the same thing: