Create TRIGGER???
CREATE TRIGGER trigCustomerName AFTER UPDATE ON "Customers"
REFERENCING NEW ROW AS newrow OLD ROW AS oldrow
FOR EACH ROW
BEGIN ATOMIC
WHEN newrow."FirstName" is not null
SET newrow."CustomerName" = newrow. "FirstName"||newrow."LastName";
END
Error:
Unexpected end of command: REFERENCING in statement [CREATE TRIGGER trigCustomerName AFTER UPDATE ON "Customers" REFERENCING]
I tried to run your suggestion and I get the error shown below. Am I using the correct process, I go to menu Tools -> SQL… and type in my code and click
CREATE TRIGGER
trigCustomerName
AFTER INSERT ON
PUBLIC."Customers"
REFERENCING
NEW ROW AS newrow
FOR EACH ROW BEGIN ATOMIC
UPDATE "Customers"
SET newrow."CustomerName" = newrow. COALESCE("FirstName",'') || newrow."LastName";
END
error:
1: Unexpected end of command: REFERENCING in statement [CREATE TRIGGER
trigCustomerName
AFTER INSERT ON
PUBLIC."Customers"
REFERENCING]
I tried to run your suggestion and I get the error shown below. Am I using the correct process, I go to menu Tools -> SQL… and type in my code and click ?
Yes, you are doing the correct process. One question – what version of HSQLDB are you using? If you are using the default version 1.8 which comes with the standard ‘embedded’ Base installation, then you’re out of luck because that version won’t run TRIGGERS. (You need HSQLDB version 2.3.x which you may use connecting Base to the HSQLDB database in ‘split’ mode.
Anyway, just delete the text PUBLIC. in the Trigger creation statement I posted before (I’ve updated it), and see what happens. You should get a message saying the TRIGGER was created successfully if it works.
Thanks for the assistance.
OK. Here’s some info on converting your Base database into ‘split’ mode.
It occurred to me though that you don’t really need to write the full name into your table, because it can always be ‘calculated’ by concatenation on-the-fly in a QUERY (like the one you are uing in the TRIGGER CREATION statement. So you mightn’t really need to worry about TRIGGERS at all! (And therefore also avoid having to do the ‘split’ mode conversion to use the latest HSQLDB version 2.3.x)
CREATE TRIGGER
trigCustomerName
AFTER INSERT ON
"Customers"
REFERENCING
NEW ROW AS newrow
FOR EACH ROW BEGIN ATOMIC
UPDATE "Customers"
SET newrow."CustomerName" = newrow.COALESCE("FirstName",'') || newrow.COALESCE("LastName",'');
END
error:
1: user lacks privilege or object not found: CustomerName
*EDIT (Mod) Please refer this thread about formatting code examples.
That message looks like it can’t find a column called CustomerName – check it does exist in the Customers table exactly as named in the CREATE TRIGGER statement?
CREATE TABLE "CUSTOMERS"
(
"ID" INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH <img src="http://en.libreofficeforum.org/sites/all/modules/smileys/packs/Roving/innocent.png" title="Innocent" alt="Innocent" class="smiley-content"/> NOT NULL PRIMARY KEY,
"FirstName" VARCHAR(20) NOT NULL,
"LastName" VARCHAR(20) NOT NULL,
"CustomerName" VARCHAR(41) GENERATED ALWAYS AS(("CUSTOMERS"."FirstName"||' ')||"CUSTOMERS"."LastName"),
"CustomerName2" VARCHAR(42) GENERATED ALWAYS AS(("CUSTOMERS"."LastName"||', ')||"CUSTOMERS"."FirstName")
)
R
Has anyone created simple triggers like this with success? If so, explain what you did.
Maybe once the new row has been inserted in your customer table (because you are using the AFTER INSERT clause), you should just reference the target table as “CustomerName” (not newrow.“CustomerName” as I suggested earlier) – so try:
CREATE TRIGGER
trigCustomerName
AFTER INSERT ON
"Customers"
REFERENCING
NEW ROW AS newrow
FOR EACH ROW BEGIN ATOMIC
UPDATE "Customers"
SET "CustomerName" = newrow.COALESCE("FirstName",'') || newrow.COALESCE("LastName",'');
END
Let us know if this does or does not work. (I have indeed got a rather similar trigger to work without a problem. )
Did you try the method of setting default values using the GENERATED ALWAYS AS syntax as suggested by F3K Total?
CREATE TRIGGER
trigCustomerName
BEFORE INSERT ON
"Customers"
REFERENCING
NEW ROW AS newrow
FOR EACH ROW BEGIN ATOMIC
SET NEWROW.NAME = COALESCE(newrow."FirstName"||' ','') ||newrow."LastName";
END
(… with a little help from my friends)
R
CREATE TRIGGER
trigCustomerName
AFTER INSERT ON
"Customers"
REFERENCING
NEW ROW AS newrow
FOR EACH ROW BEGIN ATOMIC
UPDATE "Customers"
SET "CompanyName" = COALESCE(newrow."FirstName",'') || COALESCE(newrow."LastName",'');
END
While not a practical example, a functional proof of concept is great!
Thanks again everyone!
It should be ‘commented’ for other users reading the forum, that ( user posting ) process should NOT be used. Reasons:
- It involved TWO ( or more ) operations on the table, one to INSERT data withOUT the column being inserted with the correct data ( desired text ), followed by, an UPDATE statement.
- Critical: the UPDATE statement written by the user, will, UPDATE 100% ( all ) the records in the table, NOT just the one record being INSERTED. This is very inefficient if alot of records in the table.
- Your original post . . . using a GENERATED statement, I agree is a very good solution.
- Another solution, that could have been used with HSQL 2.3.? is . . . to CREATE a VIEW . . . and . . . the VIEW would contain ALL the columns in the TABLE, and, the “CompanyName” column would be the string calculation. This way, the VIEW can be used in a form ( it contains the Primary Key ) and, INSERTed, UPDATEd, or, DELETEd as necessary.
I have tested your latest solution out on my own sample database, and yes, F3K Total is correct. Your solution updates the fullname column of ALL the rows with the the current (new) value of “CustomerName” (you have to REFRESH the table to see this result). Not what you want!
But this modified TRIGGER works on my setup:
CREATE TRIGGER
trigCustomerName
AFTER INSERT ON
"Customers"
REFERENCING
NEW ROW AS newrow
FOR EACH ROW BEGIN ATOMIC
UPDATE "Customers"
SET "CustomerName" = COALESCE(newrow."FirstName",'') || COALESCE(newrow."LastName",'') WHERE "Customers"."id"= newrow."id";
END
Note: the update is restricted to ONLY the row matching the newrow id value (using the WHERE clause). So it can be done with a TRIGGER (with HSQL 2.3.x). But I think F3K Total’s two alternative solutions are very nice also.
I also need to know about creating the Trigger command. I visited lots of site today but was unable to find suitable answer.
- You'll find on the web slot machines at a broadcasting who is especially focused just for on-line betting players.
To which question (at least with enough details to give an answer).
Try this:
Tips: Work on a COPY of your table, just in case the UPDATE does something unexpected you don’t want. The COALESCE function is used to deal with a NULL FirstName