472,958 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Before trigger question

Following up on the discussion yesterday on how to prevent the
generation on log error messages when someone tries to insert a
duplicate primary key record and Nigel Andrew's suggestion of using a
BEFORE trigger I have written the following function.

I am pretty new to triggers and function so I would appreciate and
comments or suggestions. Is this function sufficient? Can it be made
more efficient?

create or replace function pot_doc_dup_check() returns trigger as '
DECLARE pkExists BOOLEAN := false;
BEGIN
SELECT INTO pkExists EXISTS (SELECT null FROM pot_documents where
data_id=NEW.data_id);
IF pkExists = true THEN
RETURN null;
ELSE
RETURN NEW;
END IF;
END;
' LANGUAGE 'plpgsql' with (iscachable);

create trigger pot_doc_dup_check BEFORE INSERT ON pot_documents
for each ROW EXECUTE PROCEDURE pot_doc_dup_check();

TIA,

Jean-Christian Imbeault
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 11 '05 #1
2 1624
On Thu, Jul 03, 2003 at 09:28:25AM +0900, Jean-Christian Imbeault wrote:
create or replace function pot_doc_dup_check() returns trigger as '
DECLARE pkExists BOOLEAN := false;
BEGIN
SELECT INTO pkExists EXISTS (SELECT null FROM pot_documents where
data_id=NEW.data_id);
IF pkExists = true THEN
RETURN null;
ELSE
RETURN NEW;
END IF;
END;
' LANGUAGE 'plpgsql' with (iscachable);


Well, I dunno if it's more efficient, but *I think* you can write it as

BEGIN
SELECT 1 FROM pot_documents WHERE data_id=NEW.data_id;
IF FOUND THEN
RETURN null;
ELSE
RETURN NEW;
END IF;
END;

(untested)

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"In fact, the basic problem with Perl 5's subroutines is that they're not
crufty enough, so the cruft leaks out into user-defined code instead, by
the Conservation of Cruft Principle." (Larry Wall, Apocalypse 6)

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 11 '05 #2
Alvaro Herrera wrote:

Well, I dunno if it's more efficient, but *I think* you can write it as

SELECT 1 FROM pot_documents WHERE data_id=NEW.data_id;
Would a 'LIMIT 1' be useful here? But data_id is defined as a primary
key so maybe pg is smart enough to know there can only be one match and
will stop looking after it finds the first match?
(untested)


Testing now :) But would I test for speed?

Jean-Christian Imbeault
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 11 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: chandan | last post by:
hi, I have a insert trigger on a table. I want commit to happen after insert before invoking the trigger. So that if a quey is made before the completion of the trigger.Modified Data should be...
4
by: DTB | last post by:
I am having trouble creating an INSTEAD OF trigger in SQL Server to replicate a BEFORE UPDATE trigger from ORACLE. Here is a sample of the ORACLE BEFORE UPDATE trigger: CREATE TRIGGER myTRIGGER ON...
1
by: Simon Holmes | last post by:
Hi, I am having trouble calling a UDF from a 'before update' trigger whereas I have no problems calling it from the 'after update' trigger. The trigger is as below : CREATE TRIGGER foo NO...
2
by: Jack | last post by:
We are have a question about the no cascade option on before triggers. The description stays that no other triggers will be fired by the changes of a before trigger. One of our developers is...
3
by: uninfmx | last post by:
Hi If one or mode records get deleted from t1 (see below), I'd like delete all the corresponding records from t2. There is no foreign key relationship between t2 and t1, so cascading delete is...
2
by: 73blazer | last post by:
Perhaps my thinking is wrong but this is what I have: 1 table (Tab1) with 1 attribute (Attr1) Attr1 char(16) for bit data ----------------------------------------------- create trigger...
5
by: Mike Nolan | last post by:
I have a before insert trigger that updates a value in another table. It appears that I cannot depend upon that update having taken place in an after insert trigger on the first table. (The one...
5
by: wpellett | last post by:
I can not get the SQL compiler to rewrite my SQL UPDATE statement to include columns being SET in a Stored Procedure being called from a BEFORE UPDATE trigger. Example: create table...
3
by: Michel Esber | last post by:
Hello Environment: DB2 V8 LUW FP12. I have a function that returns a table. I am trying to use it inside a before trigger: create trigger TRG.T_MACHINE_RTM before insert on...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.