473,404 Members | 2,187 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,404 software developers and data experts.

trigger creating table

I want to create a function that 'on update' and 'on insert' in table A column nome_tabella create another table named as the record just added in A:
Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION trig_creazione_tabella() RETURNS trigger AS $$
  2.     BEGIN
  3.         IF (TG_OP = 'UPDATE') THEN
  4.     CREATE TABLE NEW.nome_tabella (id serial NOT NULL, sigla_comune character varying(4));
  5.         ELSIF (TG_OP = 'INSERT') THEN
  6.     CREATE TABLE NEW.nome_tabella (id serial NOT NULL, sigla_comune character varying(4));
  7.         END IF;
  8.     RETURN NEW;
  9.     END;
  10. $$ LANGUAGE plpgsql;
  11.  
on pgAdminIII i have an error on $1 (NEW.nome_tabella) and i can't understand what's the problem, so I humbly ask help.

Thanks to all
Sentenza
May 13 '10 #1

✓ answered by rski

You should use dynamic SQL

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION trig_creazione_tabella() RETURNS trigger AS $$
  2. BEGIN
  3. IF (TG_OP = 'UPDATE') THEN
  4. EXECUTE 'CREATE TABLE '||NEW.nome_tabella||' (id serial NOT NULL, sigla_comune character varying(4))';
  5. ELSIF (TG_OP = 'INSERT') THEN
  6. EXECUTE 'CREATE TABLE '||NEW.nome_tabella||' (id serial NOT NULL, sigla_comune character varying(4))';
  7. END IF;
  8. RETURN NEW;
  9. END;
  10. $$ LANGUAGE plpgsql;
  11.  

2 2531
rski
700 Expert 512MB
You should use dynamic SQL

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION trig_creazione_tabella() RETURNS trigger AS $$
  2. BEGIN
  3. IF (TG_OP = 'UPDATE') THEN
  4. EXECUTE 'CREATE TABLE '||NEW.nome_tabella||' (id serial NOT NULL, sigla_comune character varying(4))';
  5. ELSIF (TG_OP = 'INSERT') THEN
  6. EXECUTE 'CREATE TABLE '||NEW.nome_tabella||' (id serial NOT NULL, sigla_comune character varying(4))';
  7. END IF;
  8. RETURN NEW;
  9. END;
  10. $$ LANGUAGE plpgsql;
  11.  
May 13 '10 #2
perfect, now it works fine!

thank you very much!!

bye Sentenza
May 17 '10 #3

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

Similar topics

1
by: Franklin Bowen | last post by:
Are there any limitations or gotchas to updating the same table which fired a trigger from within the trigger? Some example code below. Hmmm.... This example seems to be working fine so it...
2
by: Bruce Lester | last post by:
Is there a way to create a trigger directly on an inline or multi-line table value function? I am trying to create a quick-and-dirty application using an Access Data Project front-end with SQL...
4
by: GRenard | last post by:
Hi, I'm trying just to display a table on a webpage using DOM elements created dynamically. I really don't understand why IE doesn't display the document successfully... If I make a...
0
by: Yuva | last post by:
Hi I have created a datatbase "testschema" and schema called "MySchema". I have created a table "MySchema.table1" in the database. I am trying to create sql cache dependancy on table using...
13
by: Jo | last post by:
Hi. I'm getting the following error when creating a table with 250 columns .. I have tried creating it in a 32K tablespace , still the same issue. Is this a limitation in DB2? I am using DB2...
3
by: --[zainy]-- | last post by:
AA! I am having problems on creating table through Enterprise Manager. It gives me Error 1038 i.e. is as follows Unexpected Error ODBC error: Cannot use empty object or column names. Use...
2
by: KGP | last post by:
I have written one procedure into which I am creating table(Oracle 10g) While executing procedure I get error as:ORA-01031: insufficient privileges where as this user has DBA rights and the same...
2
by: veekarthick | last post by:
I want to create a table using trigger in oracle.How it is possible...some body help me..
1
by: Ankit | last post by:
Hi guys i need to make a table to store a certain data using Tkinter..I have searched on the group but i have not been able to find a solution that would work for me..The thing is that i want my...
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: 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...
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
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,...

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.