473,382 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

INSERT BEFORE Trigger

Anytime I create an INSERT BEFORE trigger that includes a query on the
same table as the trigger is assigned, the insert does not happen
without error. I get 'INSERT 0 0'. It is like the query loses the insert
information, is this something that can't be done?

DECLARE
checkit record;
BEGIN
SELECT INTO checkit MAX(public.tblhudunits.sort_order) AS maximum,
MIN(public.tblhudunits.sort_order) AS minimum FROM public.tblhudunits
WHERE (public.tblhudunits.hud_building_id = NEW.hud_building_id);
IF FOUND THEN
IF (NEW.sort_order >= checkit.minimum AND NEW.sort_order <=
checkit.maximum) THEN
RAISE EXCEPTION 'Sort Order cannot be between % and %',
checkit.minimum, checkit.maximum;
EXIT;
END IF;
END IF;
RETURN NULL;
END;

--
Robert
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #1
2 1921

On Sat, 19 Jun 2004, Robert Fitzpatrick wrote:
Anytime I create an INSERT BEFORE trigger that includes a query on the
same table as the trigger is assigned, the insert does not happen
without error. I get 'INSERT 0 0'. It is like the query loses the insert
information, is this something that can't be done?

DECLARE
checkit record;
BEGIN
SELECT INTO checkit MAX(public.tblhudunits.sort_order) AS maximum,
MIN(public.tblhudunits.sort_order) AS minimum FROM public.tblhudunits
WHERE (public.tblhudunits.hud_building_id = NEW.hud_building_id);
IF FOUND THEN
IF (NEW.sort_order >= checkit.minimum AND NEW.sort_order <=
checkit.maximum) THEN
RAISE EXCEPTION 'Sort Order cannot be between % and %',
checkit.minimum, checkit.maximum;
EXIT;
END IF;
END IF;
RETURN NULL;


You do not want to return NULL from a before trigger generally. Returning
NULL effectively means suppress this operation. I think you probably want
RETURN NEW;
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #2

On Sat, 19 Jun 2004, Robert Fitzpatrick wrote:
Anytime I create an INSERT BEFORE trigger that includes a query on the
same table as the trigger is assigned, the insert does not happen
without error. I get 'INSERT 0 0'. It is like the query loses the insert
information, is this something that can't be done?

DECLARE
checkit record;
BEGIN
SELECT INTO checkit MAX(public.tblhudunits.sort_order) AS maximum,
MIN(public.tblhudunits.sort_order) AS minimum FROM public.tblhudunits
WHERE (public.tblhudunits.hud_building_id = NEW.hud_building_id);
IF FOUND THEN
IF (NEW.sort_order >= checkit.minimum AND NEW.sort_order <=
checkit.maximum) THEN
RAISE EXCEPTION 'Sort Order cannot be between % and %',
checkit.minimum, checkit.maximum;
EXIT;
END IF;
END IF;
RETURN NULL;


You do not want to return NULL from a before trigger generally. Returning
NULL effectively means suppress this operation. I think you probably want
RETURN NEW;
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #3

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

Similar topics

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: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
9
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following fields: WO.WONUM, VARCHAR(10) WO.PMNUM,...
4
by: brent.ryan | last post by:
How do I get the next int value for a column before I do an insert in MY SQL Server 2000? I'm currently using Oracle sequence and doing something like: select seq.nextval from dual; Then I...
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...
1
by: abhi81 | last post by:
Hello All, I have a table on which I have created a insert,Update and a Delete trigger. All these triggers write a entry to another audit table with the unique key for each table and the timestamp....
1
by: filip1150 | last post by:
I'm trying to find if there is any performance diference between explicitly using a sequence in the insert statement to generate values for a column and doing this in an insert trigger. I...
1
by: veasnamuch | last post by:
I have a problem while I create a trigger to my table. My objective is getting any change made to my table and record it in to another table . My have thousands records before I add new trigger to...
2
by: gimme_this_gimme_that | last post by:
I'm using DB2 8.1. Suppose table foo has columns name and lname: create table foo (name as varchar(200), lname as varchar(200)); Write a trigger that inserts the lower case value of name...
2
by: lenygold via DBMonster.com | last post by:
Hi Everebody: I have a table: CREATE TABLE CROSS_REFERENCE (ROW# INTEGER NOT NULL ,KEY_WORD CHAR(16) NOT NULL ,QUERY_DESCR VARCHAR(330) NOT NULL ,PRIMARY KEY (ROW#,KEY_WORD)); It is a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.