473,412 Members | 4,966 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,412 software developers and data experts.

How to set TG_ARGV values

Hi,

I need to create a table for audit. The data will come from of the triggers.
Some values are variables and need to be passing as parameters.
How to set TG_ARGV to hold that values?

Thanks.
Jun 22 '07 #1
3 26845
michaelb
534 Expert 512MB
This is a pretty dummy example, but it should give you the idea.

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION audit_proc() 
  2. RETURNS trigger AS $$
  3. DECLARE
  4.     arg_num integer;
  5.     arg_color varchar; 
  6. BEGIN
  7.     arg_num   := TG_ARGV[0];
  8.     arg_color := TG_ARGV[1];
  9.  
  10.     IF NEW.id > arg_num THEN
  11.        raise notice 'new record id % is greater than control number %', NEW.id, arg_num;
  12.     ELSE
  13.        raise notice 'new record id % is less than or equal to control number %', NEW.id, arg_num;
  14.     END IF;
  15.  
  16.     IF NEW.color IS NOT NULL AND NEW.color != arg_color THEN
  17.        -- do not allow to insert this value (see RULE and CHECK)
  18.        raise exception 'new color % is not equal to control color %', NEW.color, arg_color;
  19.     END IF;
  20.  
  21.     -- everything is OK, let's set the time and the author of this update
  22.     NEW.update_date := current_timestamp;
  23.     NEW.update_user := current_user;
  24.     return NEW;
  25. END;
  26. $$
  27. LANGUAGE plpgsql;
  28.  
  29. -----------------------------------
  30.  
  31. CREATE TRIGGER trig_audit
  32.    BEFORE INSERT OR UPDATE ON bar
  33.    FOR EACH ROW
  34.    EXECUTE PROCEDURE audit_proc(100, 'blue');
  35.  
  36.  
Jun 23 '07 #2
*** EXECUTE PROCEDURE audit_proc(100, 'blue');

If I have a Dellphi program that fired this trigger, how to change "(100, "blue")" for variables like "application user" and "current IP address" ?
Jun 24 '07 #3
michaelb
534 Expert 512MB
The trigger is fired in response to the specified action, such as INSERT, UPDATE or DELETE.
The trigger procedure (function) cannot be explicitly passed any arguments, instead it has access to NEW, OLD, TG_ARGV[] and few other special variables.
You can set the TG_ARGV[] values in the trigger that invokes the function.

I think it would help you if you review the man pages following these links.
Let us know if you have question afterwards.

Create Trigger
Trigger Procedures

By the way, you may also be interested in looking into the RULES
This is a pretty powerful facility; in two words you can say this:
Instead of update (or insert, or delete) do the following:
- check on some values, conditions, etc
- set some values if necessary
- approve or reject the update
Jun 24 '07 #4

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

Similar topics

5
by: Paul C-T | last post by:
Hi, Am I trying to be too clever here? I am trying to write a PHP page to enable me to enter values into a form then write those values to a text file. I want to use the form & table that...
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
1
by: Sue Adams | last post by:
I have a form on an asp page and am trying to write the code to place the values of all checkboxes that have been selected, into an array. The checkbox values will be used in a dynamic sql statement...
4
by: Steve Hall | last post by:
Folks, My secnario involves two tables - ObservationRegister, and Person. ObservationRegister contains most of the "useful" fields, including the UserID of the person that raised the record, and...
6
by: cipher | last post by:
I have some constant values in my web service that my client application will require. Having to keep server side and client side definitions insync is tedious. I am trying to do something like...
2
by: Hennie | last post by:
I apologise if this is a stupid question, but I would appreciated any help on this subject. I want to create a view (VIEW_1 in example below) where I take numeric values from a field in one...
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
13
by: Gregor =?UTF-8?B?S292YcSN?= | last post by:
Hi! With VALUES you can do something like: SELECT * FROM (VALUES ('A', 1), ('B', 2), ('C', 2)) AS TEMP(LETTER, NUMBER) which will give you: LETTER NUMBER ------ ------ A 1 B 2...
8
by: gigonomics | last post by:
Hi all, I hope someone can help me out. I need to return the best available seats subject to the constraint that the seats are side by side (or return X consecutive records from a table column...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.