473,385 Members | 1,409 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,385 software developers and data experts.

how-to - trigger function with argument / parameter

1
Dear all PostgreSQL xperts!

Hi, I am newbie in PostgreSQL. I am currently developing an app that use PostgreSQL as a BackEnd Database.
Now I am having problem with the function trigger since last week.

Here's the case:

create table nr_tr (tr_type character(3), period character(6), nr_tr integer);
create table h_trsale(nr_tr integer, sale_dt datetime, desc text);
create table h_trbuy (nr_tr integer, sale_dt datetime, desc text);

The table: nr_tr -> holds the latest transaction number per transaction type
(tr_type) and per book period (period).
================================================== ==========================
records the latest number of the transaction for certain types and will generate a new record and starts the transaction number from #1 again in every period (yyyymm).
tr_type = 'SAL' -> Sales transaction.
tr_type = 'BUY' -> Purchase transaction.


table: h_trbuy -> header purchase transaction
table: h_trsale -> header sales transaction
===========================================
Let say the current book period is '200801' (January 2008).
In every NEW transaction, nr_tr will record new number of transaction based on nr_tr calculation (this mimics the nextval() in SEQUENCE).
If system enter the new Book period ie. Feb. 2008 ('200802'), the new transaction

number will starts all over again from number 1.

I try to code a 'SEQUENCE-alike' function trigger:

create or replace function gen_nrtr(character(3), character(6))
returns "trigger" as $gen_nrtr$
declare
tcTR_TYPE %1;
tcPERIOD %2;

begin
select * from nr_tr where tr_type = tcTR_TYPE AND PERIOD = tcPERIOD;
IF ROW_COUNT == 0 THEN
INSERT INTO nr_tr VALUES (tcTR_TYPE, tcPERIOD, 0);
END IF

UPDATE nr_tr
SET nr_tr = nr_tr + 1
WHERE nr_tr where tr_type = tcTR_TYPE AND PERIOD = tcPERIOD;
RETURN NULL;

END; $gen_nrtr$ LANGUAGE 'plpgsql' VOLATILE;
ALTER FUNCTION gen_nrtr() OWNER TO postgres;


But the system won't allow it b'coz arguments are not allowed in trigger.
Well, is there any other way to make this happen? If it's in FoxPro, there won't be

any problem with the trigger function that using the arguments.

Does anybody out there could help?

Thank's in advance...


Regards,



djDevX
Jan 26 '08 #1
1 7248
rski
700 Expert 512MB
Dear all PostgreSQL xperts!

Hi, I am newbie in PostgreSQL. I am currently developing an app that use PostgreSQL as a BackEnd Database.
Now I am having problem with the function trigger since last week.

Here's the case:

create table nr_tr (tr_type character(3), period character(6), nr_tr integer);
create table h_trsale(nr_tr integer, sale_dt datetime, desc text);
create table h_trbuy (nr_tr integer, sale_dt datetime, desc text);

The table: nr_tr -> holds the latest transaction number per transaction type
(tr_type) and per book period (period).
================================================== ==========================
records the latest number of the transaction for certain types and will generate a new record and starts the transaction number from #1 again in every period (yyyymm).
tr_type = 'SAL' -> Sales transaction.
tr_type = 'BUY' -> Purchase transaction.


table: h_trbuy -> header purchase transaction
table: h_trsale -> header sales transaction
===========================================
Let say the current book period is '200801' (January 2008).
In every NEW transaction, nr_tr will record new number of transaction based on nr_tr calculation (this mimics the nextval() in SEQUENCE).
If system enter the new Book period ie. Feb. 2008 ('200802'), the new transaction

number will starts all over again from number 1.

I try to code a 'SEQUENCE-alike' function trigger:

create or replace function gen_nrtr(character(3), character(6))
returns "trigger" as $gen_nrtr$
declare
tcTR_TYPE %1;
tcPERIOD %2;

begin
select * from nr_tr where tr_type = tcTR_TYPE AND PERIOD = tcPERIOD;
IF ROW_COUNT == 0 THEN
INSERT INTO nr_tr VALUES (tcTR_TYPE, tcPERIOD, 0);
END IF

UPDATE nr_tr
SET nr_tr = nr_tr + 1
WHERE nr_tr where tr_type = tcTR_TYPE AND PERIOD = tcPERIOD;
RETURN NULL;

END; $gen_nrtr$ LANGUAGE 'plpgsql' VOLATILE;
ALTER FUNCTION gen_nrtr() OWNER TO postgres;


But the system won't allow it b'coz arguments are not allowed in trigger.
Well, is there any other way to make this happen? If it's in FoxPro, there won't be

any problem with the trigger function that using the arguments.

Does anybody out there could help?

Thank's in advance...


Regards,



djDevX

I'm sorry you wrote so much text so I did'n read it all, but who told you trigger can't have arguments see here
http://www.postgresql.org/docs/8.2/i...tetrigger.html
and here
http://www.postgresql.org/docs/8.2/i...l-trigger.html

was it helpful?
Jan 26 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tom Szabo | last post by:
Just wandering how can you close a browser remotely. In some web applications the browser closes or refreshes periodically. How is that done and how can it be done through PHP? TIA, Tom
15
by: Reid Nichol | last post by:
Hello, I was wondering if I could control how many bytes are in an int and the byte order. In C/C++ I can use int32 but how do I do this in python? How can I control byte order?
0
by: v I n O | last post by:
hI Geeks Please do let me know how do i find how many instances sql server running on the single machine. or in the n/w my objective should with help of C#/vb.net
4
by: James Salisbury | last post by:
Hi, I was checking my parent's website salisbury.cabrera.net on google by entering villa rent spain cabrera I can see the required site, ranked at 4, but I am concerend by the return at 1 and 5...
2
by: Frances Del Rio | last post by:
http://www.emol.com/especiales/cocina_chilena/comida.asp this page is so neat (goes in pop-up..) how was this done? how do you give a layer (or div, I guess) a semi-transparency so you can still...
4
by: | last post by:
Developing, building, and testing. How do it the best? Learning from the world leader - Microsoft I'm very interested in how the developing/build/testing workflow @ Microsoft looks like. I...
4
by: Supra | last post by:
in vb6 listbox1.remove item 5 how will i do in vb.net? regards
7
by: anushhprabu | last post by:
#define q(k)main(){ return!puts(#k"\nPRABUq("#k")");} q(#define q(k)main(){return!puts(#k"\nq("#k")");}) guys i'm working on this code.. i got it fromnet.. how this is working.. anyone pls.....
0
by: candra | last post by:
Learn What Hackers Know? -General Hacking Information -Password Security -Scanning, Fingerprinting And Similar Techniques -How Hackers Attack Numerous Internet Services -How Hackers Attack Web...
2
by: belred | last post by:
i just read this blog about how many objects (types) are loaded for a hello world program in C#. http://blogs.msdn.com/abhinaba/archive/2008/09/15/how-many-types-are-loaded-for-hello-world.aspx ...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.