pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedtrigger to insert on update to non-existing row?
2+ messages / 1 participants
[nested] [flat]
* trigger to insert on update to non-existing row?
@ 1999-06-18 21:52 Doug Younger <postgres@mindspring.com>
0 siblings, 0 replies; 2+ messages in thread
From: Doug Younger @ 1999-06-18 21:52 UTC (permalink / raw)
To: pgsql-sql
Hello,
I want to create a trigger/procedure to insert the values if I try to
update a row that doesn't exist...
example:
hours_table:
login varchar(16),
hour_type int4,
hours float4
So if I try:
UPDATE hours_table SET hours = hours + 8 WHERE login = 'johndoe' AND
hour_type = 10;
I want it to check:
if a row exists "WHERE login = 'johndoe' AND hour_type = 10" {
do the update.
} else {
INSERT INTO hours_table VALUES('johndoe',10,0);
& do the update
}
I've created a procedure that will take input of login & hour_type and
return hours:
CREATE FUNCTION get_hours(text,int4) RETURNS float4 AS
'SELECT hours FROM hours_table WHERE login = $1 AND hour_type = $2;'
LANGUAGE 'sql';
That part works.
I think the trigger should look like this:
CREATE TRIGGER upd_hrs BEFORE ON UPDATE ON hours_table
FOR EACH ROW EXECUTE PROCEDURE need_insert();
What should need_insert() look like? How do I get the values for login and
hour_type out of the update ?
CREATE FUNCTION need_insert() RETURNS bool AS
'
declare
hours float4;
begin
hours = get_hours($1,$2);
if hours ISNULL then
INSERT INTO hours_table VALUES($1,$2,0);
return false;
end if;
return true;
end;
'
LANGUAGE 'plpgsql';
Thanks,
Doug.
^ permalink raw reply [nested|flat] 2+ messages in thread
* trigger to insert on update to non-existing row?
@ 1999-06-26 04:31 Doug Younger <postgres@mindspring.com>
0 siblings, 0 replies; 2+ messages in thread
From: Doug Younger @ 1999-06-26 04:31 UTC (permalink / raw)
To: pgsql-sql
Hello,
This is my second attempt to get some sort of help on this.
I want to create a trigger/procedure to insert the values if I try to
update a row that doesn't exist...
example:
hours_table:
login varchar(16),
hour_type int4,
hours float4
So if I try:
UPDATE hours_table SET hours = hours + 8 WHERE login = 'johndoe' AND
hour_type = 10;
I want it to check:
if a row exists "WHERE login = 'johndoe' AND hour_type = 10" {
do the update.
} else {
INSERT INTO hours_table VALUES('johndoe',10,0);
& do the update
}
I've created a procedure that will take input of login & hour_type and
return hours:
CREATE FUNCTION get_hours(text,int4) RETURNS float4 AS
'SELECT hours FROM hours_table WHERE login = $1 AND hour_type = $2;'
LANGUAGE 'sql';
That part works.
I think the trigger should look like this:
CREATE TRIGGER upd_hrs BEFORE ON UPDATE ON hours_table
FOR EACH ROW EXECUTE PROCEDURE need_insert();
What should need_insert() look like? How do I get the values for login and
hour_type out of the update ?
CREATE FUNCTION need_insert() RETURNS bool AS
'
declare
hours float4;
begin
hours = get_hours($1,$2);
if hours ISNULL then
INSERT INTO hours_table VALUES($1,$2,0);
return false;
end if;
return true;
end;
'
LANGUAGE 'plpgsql';
I'm using 6.4.2
Thanks,
Doug.
-Doug
"Any sufficiently advanced technology is indistinguishable from magic."
-> Arthur C. Clarke
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~1999-06-26 04:31 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-06-18 21:52 trigger to insert on update to non-existing row? Doug Younger <postgres@mindspring.com>
1999-06-26 04:31 trigger to insert on update to non-existing row? Doug Younger <postgres@mindspring.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox