Create History Trigger Db2 - db2

I am trying to create a trigger that copys a newly INSERTED and/or UPDATED record from one table to a History version of this table. The purpose is to keep a record every time a record in a table is inserted and/or updated.
This is the Create Trigger statement syntax that I have, following the IBM Db2 Trigger site.
CREATE TRIGGER "TADB2"."HIST_CSEMASTER_Z_LOG"
AFTER UPDATE ON TADB2.CSEMASTER_Z
REFERENCING NEW AS N
FOR EACH ROW
BEGIN ATOMIC
INSERT INTO TADB2.HIST_CSEMASTER_Z
values (N.CSEMASTERID, N.CURRID, N.SUBCURRID, N.EXPIREDATE, N.CREATEDATE, N.CUSTVIEWNUM, N.CSEMSTRNAME, N.JOINCODE, N.CONTACTCOUNTRYCD, N.CONTACTSERIALNUM, N.AUDIENCECD, N.SPONSORNAME, N.AVAILABLEDATE, N.OWNERCOUNTRYCD, N.OWNERSERIALNUM, N.PRMWWCHAPTERID, N.PRMWWSUBCHAPTERID, N.SECWWCHAPTERID, N.SECWWSUBCHAPTERID, N.INTRANETIND, N.TINSERTTS, N.TLASTUPDTS, N.IBM_TRADEMARK, N.OTHER_TRADEMARK, N.INTERNETIND, N.BRANDID, N.BUSINESSID, N.LIFECYCLE_STATUS);
END;
The error I am receiving is:
[Code: -104, SQL State: 42601] An unexpected token "END-OF-STATEMENT" was found following "N.LIFECYCLE_STATUS)". Expected tokens may include: "".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.72.44
I've tried:
Removing the END
Removing the semi-colon after the END
Removing the semi-colon after the N.LIFECYCLE_STATUS);
Any help would be great! Thank you.

I highly recommend Db2 Temporal Tables for that. If you just use System Temporal functionality it would provide already the functionality you describe.
Worth to check it out

You must use different from ';' statement terminator in your tool.
Below is an example for DB2 Command Line Processor, which understands the first line as a directive to do this.
--#SET TERMINATOR #
CREATE TRIGGER "TADB2"."HIST_CSEMASTER_Z_LOG"
AFTER UPDATE ON TADB2.CSEMASTER_Z
REFERENCING NEW AS N
FOR EACH ROW
BEGIN ATOMIC
INSERT INTO TADB2.HIST_CSEMASTER_Z
values (N.CSEMASTERID, N.CURRID, N.SUBCURRID, N.EXPIREDATE, N.CREATEDATE, N.CUSTVIEWNUM, N.CSEMSTRNAME, N.JOINCODE, N.CONTACTCOUNTRYCD, N.CONTACTSERIALNUM, N.AUDIENCECD, N.SPONSORNAME, N.AVAILABLEDATE, N.OWNERCOUNTRYCD, N.OWNERSERIALNUM, N.PRMWWCHAPTERID, N.PRMWWSUBCHAPTERID, N.SECWWCHAPTERID, N.SECWWSUBCHAPTERID, N.INTRANETIND, N.TINSERTTS, N.TLASTUPDTS, N.IBM_TRADEMARK, N.OTHER_TRADEMARK, N.INTERNETIND, N.BRANDID, N.BUSINESSID, N.LIFECYCLE_STATUS);
END#

Related

Rewrting Triggers from Oracle to PostgreSQL

I have to rewrite code that was generated in TOAD DATA MODELER on Oracle Data Base to PostgreSQL. I have two examples:
CREATE OR REPLACE TRIGGER ts_Kluby_jezdzieckie_KlubyJe_0 BEFORE INSERT
ON Kluby_jezdzieckie FOR EACH ROW
BEGIN
:new.ID_klubu := KlubyJezdzieckieS.nextval;
END;
And the second one:
CREATE OR REPLACE TRIGGER tsu_Adresy_AdresyS AFTER UPDATE OF ID_adresu
ON Adresy FOR EACH ROW
BEGIN
RAISE_APPLICATION_ERROR(-20010,'Cannot update column ID_adresu in table Adresy as it uses sequence.');
END;
I don't understand PostgreSQL's triggers yet, and i'm hoping for some help, have a great evening, Adam
The first one is assigning a to value to ID_klubu from the sequence KlubyJezdzieckieS. In Postgres this would be
:new.ID_klubu = nextval('KlubyJezdzieckieS');
However, the trigger is not needed. In Postgres v10 and later define the column as generated identity. This would accomplish the same and eliminates the trigger.
The second raises a user defined exception where the SQLCode -20010 is a user defined exception Postgres would be:
Raise Exception '-20010, Cannot update column ID_adresu in table Adresy as it uses sequence.';
Even better define the column as
ID_adresu integer generated always as identity
and just eliminate the trigger.

Creating a trigger gives "function does not exist" error in PostgreSQL

I'm working on a project right now where I'm trying to build a trigger that updates a column "lastedit" (in the table Person), which is a timestamp storing when the last change was made to the table in the schema specified in the trigger creation (in this case Certifications).
Now, my problem is that when I try to create the trigger, after creating the function "update_lastedit()", it gives me an error saying that the function does not exist. I think I might have a mismatch in my function somewhere, but I cannot seem to find it.
Could any of you help me out? I'm running PostgreSQL 9.5.5. Please let me know if I need to give a more extensive explanation, this is my first question, so I might have overlooked something important.
My code for the trigger and the function are as follows:
CREATE OR REPLACE FUNCTION update_lastedit() RETURNS TRIGGER AS
$update_edit$
BEGIN
UPDATE ovsoftware.person
SET lastedit = now();
END;
$update_edit$
LANGUAGE plpgsql;
and
CREATE TRIGGER cert_edit_trigger
BEFORE INSERT OR UPDATE ON ovsoftware.certifications
FOR EACH ROW
EXECUTE PROCEDURE update_lastedit();
The exact error:
SQL fout:
ERROR: function update_lastedit() does not exist
In statement:
CREATE TRIGGER cert_edit_trigger
BEFORE INSERT OR UPDATE ON ovsoftware.certifications
FOR EACH ROW
EXECUTE PROCEDURE update_lastedit();
The solution was to use a fully qualified name for the function as follows: ovsoftware.update_lastedit(). I am not sure why that is the case, as I did not need to do so in other cases.
Either way, the scope apparently did not include the ovsoftware schema, leading to the error.

An unexpected token "CREATE TRIGGER

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:

increasing entry number automatically for every insert on the entry table

i tried using this code:
CREATE OR REPLACE FUNCTION eno_inc() RETURNS trigger AS $eno_inc$
BEGIN
NEW.eno := OLD.eno + 1;
END;
$eno_inc$
LANGUAGE plpgsql;
but anytime i run it generates this error:
ERROR: record "old" is not assigned yet
DETAIL: The tuple structure of a not-yet-assigned record is indeterminate.
CONTEXT: PL/pgSQL function "eno_inc" line 4 at assignment
*** Error ***
ERROR: record "old" is not assigned yet
SQL state: 55000
P.S i am very new to SQL
You are missing a
RETURN NEW;
at the end.
The error
ERROR: record "old" is not assigned yet SQL state: 55000
does show that this MIGHT not be a trigger (OLD and NEW are only used in trigger context). You need to define a trigger for that to work. I suggest you read the docs about trigger functions.
As the comment by Steve C notes, make sure you don't just need a SERIAL (auto-increment in MySQL terms).
In addition to what #DrColossos already wrote ..
OLD and NEW are only defined in a trigger ON UPDATE. In a trigger on INSERT you would get this error message. In a trigger on DELETE you only have OLD at your disposal.
Also, for a trigger to work you need a trigger function plus a trigger. You may be missing that, too. Start by reading the excellent manual here.
But I suspect your problems are more basic than that. First of all, as #Steve commented, you are probably just looking for a serial column or a sequence and a DEFAULT to your already existing column.

DB2 Equivalent to SQL's GO?

I have written a DB2 query to do the following:
Create a temp table
Select from a monster query / insert into the temp table
Select from the temp table / delete from old table
Select from the temp table / insert into a different table
In MSSQL, I am allowed to run the commands one after another as one long query. Failing that, I can delimit them with 'GO' commands. When I attempt this in DB2, I get the error:
DB2CLI.DLL: ERROR [42601] [IBM][CLI Driver][DB2] SQL0199N The use of the reserved
word "GO" following "" is not valid. Expected tokens may include: "".
SQLSTATE=42601
What can I use to delimit these instructions without the temp table going out of scope?
GO is something that is used in MSSQL Studio, I have my own app for running upates into live and use "GO" to break the statements apart.
Does DB2 support the semi-colon (;)? This is a standard delimiter in many SQL implementations.
have you tried using just a semi-colon instead of "GO"?
This link suggests that the semi-colon should work for DB2 - http://www.scribd.com/doc/16640/IBM-DB2
I would try wrapping what you are looking to do in BEGIN and END to set the scope.
GO is not a SQL command, it's not even a TSQL command. It is an instruction for the parser. I don't know DB2, but I would imagine that GO is not neccessary.
From Devx.com Tips
Although GO is not a T-SQL statement, it is often used in T-SQL code and unless you know what it is it can be a mystery. So what is its purpose? Well, it causes all statements from the beginning of the script or the last GO statement (whichever is closer) to be compiled into one execution plan and sent to the server independent of any other batches.