473,473 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pl/pgsql triggers - Problem with NEW.*

20 New Member
Hello there

I have been trying to generate a dynamic querystring thats to be executed inside a trigger function and every time I use NEW.* pl/pgsql throws an error saying 'column * does not exist'.

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION matrix_test_insert_trigger()
  2.   RETURNS trigger AS
  3. $$
  4.  
  5.  BEGIN
  6.  
  7.     EXECUTE 'INSERT INTO matrix_' || NEW.month || '(id,accept,day,month,year) values ' || NEW.*;
  8.  
  9.  RETURN NULL;
  10.  END;
  11. $$
  12.   LANGUAGE 'plpgsql'
Since this did not work I tried to alter it a bit by explicitly specifying the column names like

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION matrix_test_insert_trigger()
  2.   RETURNS trigger AS
  3. $$
  4.  
  5.  BEGIN
  6.  
  7.     EXECUTE 'INSERT INTO matrix_' || NEW.month || '(id,accept,day,month,year) values ' || (NEW.id,NEW.accept,NEW.day,NEW.month,NEW.year);
  8.  
  9.  RETURN NULL;
  10.  END;
  11. $$
  12.   LANGUAGE 'plpgsql'

It works fine for non-character columns, I tried to insert values like "INSERT into matrix(id,accept,day,month,year) values (4,'YES',3,3,2007)", but pl/pgsql throws an error for 'YES'. This is because it ignores the quotes when I specify the column names explicitly.

Could anyone please help me out with this? And if you are wondering why the table names are different, they are partitioned tables so there is a child-parent relationship between them.

Any help on this issue would be appreciated!

Cheers
Mar 4 '08 #1
4 4502
rski
700 Recognized Expert Contributor
Try like this
Expand|Select|Wrap|Line Numbers
  1.  CREATE OR REPLACE FUNCTION matrix_test_insert_trigger()
  2.  RETURNS trigger AS$$
  3.  BEGIN
  4.      EXECUTE 'INSERT INTO matrix_' || NEW.month || (id,accept,day,month,year) values( ' || NEW.id ||','|| NEW.accept ||','|| NEW.day ||','|| NEW.month ||','|| NEW.year||')';
  5.  
  6.       RETURN NULL;
  7.  END;
  8.  $$ LANGUAGE 'plpgsql'
  9.  
Does it work for you?
Mar 5 '08 #2
pravinasp
20 New Member
Thanks for your resply rski. I was having a play with it and figured out the following works,

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION matrix_test_insert_trigger()
  2. RETURNS trigger AS $$ 
  3. BEGIN
  4.    EXECUTE 'INSERT INTO matrix_' || NEW.month || '(id,accept,day,month,year) values(' || NEW.id ||,'''|| NEW.accept ||''',|| NEW.day ||,|| NEW.month ||,|| NEW.year||')';
  5.    RETURN NULL;
  6. END;  
  7. $$ LANGUAGE 'plpgsql'
although I would like to know why NEW.* wont work. Its purely because I have around 65 columns and I dont want to go through each of them by hand.

I know something like the following is possible,

Expand|Select|Wrap|Line Numbers
  1.  IF (NEW.month = 03 and NEW.year=2007) THEN
  2.     INSERT INTO landings_2007_03 values (NEW.*);
I use this for a different table, I would like to know if there is a way to achieve something like above when building a dynamic query with NEW.*

Cheers
Mar 6 '08 #3
rski
700 Recognized Expert Contributor
try function like this
Expand|Select|Wrap|Line Numbers
  1. create or replace function instead_ins() returns trigger as $$
  2. declare
  3. r record;
  4. t text;
  5. x text;
  6. y text;
  7. begin
  8.         select NEW.* into r;
  9.         select r into x;
  10.         y=substring(x from 2 for length(x)-2);
  11.         t='table_prefix_'||NEW.column_name;
  12.         execute 'insert into '||t||' values( '||y||')';
  13.         return NULL;
  14.  
  15. end;
  16. $$ language 'plpgsql';
  17.  
is it helpful?
Mar 9 '08 #4
pravinasp
20 New Member
Thanks again for your reply rski.

The function you had posted is a cool work around although I get the following error ,

Syntax error at or near ","
insert into matrix_10 values(2,,,,,,,2007,19,10,,,,,,,,,,,name,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)

I guess its because record type is looking for data for all columns..?? Or is it me overlooking something?

Cheers
Mar 11 '08 #5

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

Similar topics

1
by: D. Dante Lorenso | last post by:
I just wrote a PL/PGSQL function that is working, but I don't know why it is... I have a foreign key constraint defined on: transaction.invoice_id --> invoice.invoice_id But I did NOT state...
34
by: Karam Chand | last post by:
Hello I have been working with Access and MySQL for pretty long time. Very simple and able to perform their jobs. I dont need to start a flame anymore :) I have to work with PGSQL for my...
4
by: Postgresql | last post by:
Hello, I am unable to get on many of the postgresql mailing lists. I usually get this message back with no attachment and no email comes afterwards with a confirmation. I've tried this over and...
1
by: Graeme Hinchliffe | last post by:
Hiya, Not had much experience with tiggers under postgres but am liking them so far. My problem is this. I am writing an updates system, postgres holds the master copy of the database, any...
2
by: Henriksen, Jonas F | last post by:
Hi, I'm writing some simple triggers and functions for a postgres database, andI'm wondering how to go about to debug a pl/pgSQL-script. Is there a way to echo variable-content to screen, or to...
7
by: snpe | last post by:
Is it down ? regards ---------------------------(end of broadcast)--------------------------- TIP 9: the planner will ignore your desire to choose an index scan if your joining column's...
2
by: Tim Vadnais | last post by:
Hi, My boss wants to add some logging functionality to some of our tables on update/delete/insert. I need to log who, when, table_name, field name, original value and new value for each record,...
1
by: sysxperts | last post by:
Hello, Having an issue that is specific to PHP compiled with PGSQL support with versions noted in subject line. I understand that there are many variables to consider here but believe I have...
1
by: ghe | last post by:
Good afternoon to all, I have another inquiry about PHP, I just wanted to ask how can i create TRIGGERS in PostgreSQL (PGSQL) via PHP? Does PHP offers other functions with the same...
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...
1
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
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...
0
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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 ...

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.