Create TRIGGER???

17 replies [Last post]
crecker
Offline
Last seen: 1 year 34 weeks ago
Title:
Joined: 14 Sep 2014
Posts: 7
Can someone tell me what I am doing wrong with my Create Trigger command.
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] 
frofa
frofa's picture
Offline
Last seen: 13 hours 24 min ago
Title: ★★★★★
Joined: 24 Feb 2012
Posts: 804
Trigger example
crecker:

Try this:


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."LastName";
END

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

crecker
Offline
Last seen: 1 year 34 weeks ago
Title:
Joined: 14 Sep 2014
Posts: 7
Trigger
frofa:

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] 
frofa
frofa's picture
Offline
Last seen: 13 hours 24 min ago
Title: ★★★★★
Joined: 24 Feb 2012
Posts: 804
What version of HSQLDB are you using?
crecker:

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.

crecker
Offline
Last seen: 1 year 34 weeks ago
Title:
Joined: 14 Sep 2014
Posts: 7
1.8 Damn
Between post that thought occurred to me and started looking into it. I’m using the default version with LifbreOffice 4.2.6.3

Thanks for the assistance.

frofa
frofa's picture
Offline
Last seen: 13 hours 24 min ago
Title: ★★★★★
Joined: 24 Feb 2012
Posts: 804
Do you really need to use a TRIGGER anyway?
crecker:

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)

crecker
Offline
Last seen: 1 year 34 weeks ago
Title:
Joined: 14 Sep 2014
Posts: 7
Still need help
Okay, I followed the tutorial at the link you provided. Thank you. So I have my split database, recreated my table, entered some test data. All of that went fine. Now I am trying to create this trigger and I get the error below. Any help would be great! (I know I can put these names together in a query, this is more of a proof of concept to get triggers working for other things down the line.)
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)Sexy Please refer this thread about formatting code examples.

crecker
Offline
Last seen: 1 year 34 weeks ago
Title:
Joined: 14 Sep 2014
Posts: 7
Any suggestions?
Any suggestions?
frofa
frofa's picture
Offline
Last seen: 13 hours 24 min ago
Title: ★★★★★
Joined: 24 Feb 2012
Posts: 804
CREATE TRIGGER statement error sleuthing
crecker:

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?

F3K Total
Offline
Last seen: 2 weeks 6 hours ago
Title: ★★★★★
Joined: 14 Oct 2013
Posts: 201
Any suggestions?
Hi, if you’re using HSQL23x, you don’t need a trigger, you can generate the column e.g.

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

crecker
Offline
Last seen: 1 year 34 weeks ago
Title:
Joined: 14 Sep 2014
Posts: 7
wtf
I’m at a loss. I followed the tutorial. Downloaded the db with the macro. Opened the split db I downloaded, allowed the macro to run, for simplicity sake I created a Customers table using the wizard, tried creating the trigger with CompanyName (again keeping it simple) and I still get the error saying I lack privilege or object not found.

Has anyone created simple triggers like this with success? If so, explain what you did.

frofa
frofa's picture
Offline
Last seen: 13 hours 24 min ago
Title: ★★★★★
Joined: 24 Feb 2012
Posts: 804
Try a slightly different synax....
Hi crecker:

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?

F3K Total
Offline
Last seen: 2 weeks 6 hours ago
Title: ★★★★★
Joined: 14 Oct 2013
Posts: 201
Trigger
Hi, if you’re using HSQLDB 2.x then you can use this:

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

crecker
Offline
Last seen: 1 year 34 weeks ago
Title:
Joined: 14 Sep 2014
Posts: 7
Thank You
Thanks to everyone for the assistance. Here is what finally worked.

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!

F3K Total
Offline
Last seen: 2 weeks 6 hours ago
Title: ★★★★★
Joined: 14 Oct 2013
Posts: 201
No good solution
Comment from a friend:

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.
frofa
frofa's picture
Offline
Last seen: 13 hours 24 min ago
Title: ★★★★★
Joined: 24 Feb 2012
Posts: 804
A trigger that works
crecker:

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.

Andent
Offline
Last seen: 1 year 7 weeks ago
Title:
Joined: 23 Jan 2015
Posts: 6
I also need to know about

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.
eremmel
Offline
Last seen: 5 days 20 hours ago
Title: ★★★★★
Joined: 15 Dec 2011
Posts: 276
Answer to which question
“I visited lots of site today but was unable to find suitable answer.”

To which question (at least with enough details to give an answer).

n/a

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.