473,729 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PL/TCL trigger error "can't read "tgname": no such variable"

2 New Member
Hi all,
I am trying to get a PL/TCL trigger to update an audit table working. The problem is when I create the trigger on an exiting table, I get the following error

"ERROR: can't read "tgname": no such variable
CONTEXT: can't read "tgname": no such variable
while executing ....."

If I drop the table and then recreate the table and add the rigger, the trigger works fine. However if I alter the table, again I have to drop and re-create the bale and triggers.

My problem is I have over 60 table to add the triggers to and each time I implement a change I can not have the tables to be dropped and recreated.

Following is sample of my tcl function

CREATE OR REPLACE FUNCTION xxx.log_to_audi t_table()
RETURNS "trigger" AS
$BODY$


spi_exec "SELECT CURRENT_USER AS tguser"


spi_exec "SELECT c.relname AS tgname,n.nspnam e AS schema_name
FROM pg_class c , pg_namespace n
WHERE n.oid = c.relnamespace
AND relfilenode = $TG_relid"



spi_exec "insert into xxx.testlogtabl e values ('inserted in log_to_audit_ta ble $tgname')";

set pk_name ""

spi_exec "SELECT a.attname AS pk_name FROM pg_class c, pg_attribute a, pg_index i
WHERE c.relname = '$tgname'
AND c.oid=i.indreli d
AND a.attnum > 0
AND a.attrelid = i.indexrelid
AND i.indisprimary= 't'"

spi_exec "SELECT audit_table_nam e AS m_aud_tbl_name
FROM $main_schema.au dit_table_mappi ng
where schema_name = '$schema_name'
and tabel_name = '$tgname'"


.....

return OK
$BODY$
LANGUAGE 'pltcl' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

and this is my trigger

CREATE TRIGGER trig_audit_admi n_user
AFTER INSERT OR UPDATE OR DELETE
ON xxx."user"
FOR EACH ROW
EXECUTE PROCEDURE "xxx"."log_to_a udit_table"();

The problem is the error comes where I am trying to use the values queried from the catalogue $tgname, $schema_name etc...

Any help would be much apprciated
Jul 6 '10 #1
1 4237
indikamaligaspe
2 New Member
Guys,
I found the solution to this, sorry was now able to post s reply till now.
The issue is in the following code

spi_exec "SELECT c.relname AS tgname,n.nspnam e AS schema_name
FROM pg_class c , pg_namespace n
WHERE n.oid = c.relnamespace
AND relfilenode = $TG_relid"

When you create a table the pg_class table gets a record for that table and the OID and relfilenode are the same in that record. When you create the trigger, the trigger gets mapped to the OID of the table in pg_class.

So in effect the $TG_relid is equal to both OID and relfilenode in the pg_class table.

However when you do modifications to the table (change column types / add constraints etc... the "relfilenod e" value keeps on changing to match the physical data location. So when I run the query the $TG_relid no longer matches to "relfilenod e".

So I changed the code to check the OID in pg_class rather then the "relfilenod e" and that fixed the problem.
Jul 18 '10 #2

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

Similar topics

4
2430
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from this app: =============================== static void Main(string args) { if (args.Length > 0) Console.SetIn(new StreamReader(args)); //executes if I don't use the "<", ">" redirection syntax when invoking XmlTextReader xmlin = new...
33
4773
by: coosa | last post by:
I have a table: ---------------------------------------------------- CREATE TABLE CATEGORY ( CATEGORY_ID INTEGER IDENTITY(1,1) NOT NULL, CATEGORY_NAME VARCHAR(40) NOT NULL, PARENT_CATEGORY_ID INTEGER, CATEGORY_ICON IMAGE, DEPTH INTEGER,
13
2875
by: Nigel J. Andrews | last post by:
This will be a little vague, it was last night and I can't now do the test in that db (see below) so can't give the exact wording. I seem to remember a report a little while ago about tsearch v2 causing unexpected backend exit messages with 7.3.4 and now I'm getting similar messages unpredictably and I can't find the thread in the archives either. What I did was install tsearch2 using share/contrib/tsearch2.sql, which placed everything...
9
3203
by: Raquel | last post by:
Following is a simple trigger definition: CREATE TRIGGER TRIG_EMPLOYEE_1 AFTER UPDATE OF SALARY ON DB2ADMIN.EMPLOYEE FOR EACH STATEMENT MODE DB2SQL CALL DB2ADMIN.SQLSP10( ) This gives an error: SQL0104N An unexpected token "CALL" was found following "ATEMENT MODE
12
4761
by: Bob Stearns | last post by:
I am trying to create a duplicate prevention trigger: CREATE TRIGGER is3.ard_u_unique BEFORE UPDATE OF act_recov_date ON is3.flushes REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL WHEN (N.act_recov_date IS NOT NULL) BEGIN ATOMIC select count(*) into v_n from is3.flushes
3
5865
by: Madhu | last post by:
Hi, I need to trap EAGAIN error while using read(). I read in some website that read() has to wait till write() is completed for EAGAIN to occur. But read() doesnt seem to wait, it reads whatever it can and stores it in the buffer and sets the rest of the buffer to NULL. Thanks,
1
1144
by: umesh049 | last post by:
hello I want to update in one trigger the tables in to another schema but it doesn't work but when i select data from sql prompt it works fine. can any body help me. thanks. CREATE OR REPLACE TRIGGER Product_Detail_TR
1
2179
by: tb1 | last post by:
Hi, I have written a trigger and now im getting this error when i try to add it to my database. ERROR 1415 (0A000): Not allowed to return a result set from a trigger What does that mean?
1
2866
by: amijai11 | last post by:
I am getting following error for a trigger, please let me know if anyone knows how to fix the problem. Thks in advance for the help. SQLCODE = -20100, ERROR: AN ERROR OCCURRED WHEN BINDING A TRIGGERED SQL STATEMENT. INFORMATION RETURNED: SECTION NUMBER : 2 SQLCODE -104, SQLSTATE 42601, AND MESSAGE TOKENS -,TABLE STATEMENT , . SCROLL INSENSITIVE SENSITIVE ASENSITIVE NO --#SET TERMINATOR ~ CREATE TRIGGER CWSRCAL0 AFTER INSERT...
0
8927
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8764
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9289
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9210
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9156
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8159
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3242
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.