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

Create a table using triggers

2
Hi,

I have a table called A (say) with columns called name, place, animal and thing.
I would want to write an on insert trigger on this table, which would create a table with the name of the value entered in the name column.

Let's say a new column is entered with the value of name column as mickey. I want to create a table called mickey, with the help of triggers.

I'm really new to this and i would like some help. Thanks in advance for this help!!

I know the syntax..which may go like this

CREATE TRIGGER <trigger_name>
AFTER INSERT ON INST1.A
REFERENCING NEW AS NEW
FOR EACH ROW MODE DB2SQL
CREATE TABLE INST1.NEW.NAME
--Name is a column in the table
(
ID LONG NOT NULL PRIMARY_KEY,
);

Is this a right syntax and can i use AND or something to write another CREATE TABLE statement below. Like, say i have to create another table with the value under place column, can i do this?

CREATE TRIGGER <trigger_name>
AFTER INSERT ON INST1.A
REFERENCING NEW AS NEW
FOR EACH ROW MODE DB2SQL
CREATE TABLE INST1.NEW.NAME
--Name is a column in the table
(
ID LONG NOT NULL PRIMARY_KEY,
)
AND
CREATE TABLE INST1.NEW.PLACE
--Place is a column in the table
(
ID LONG NOT NULL PRIMARY_KEY,
);

Or do i have to write another trigger? Can i have more than one after insert triggers on the same table with different names to the triggers?

Thanks in advance to all those who help...

Thanks once again,
Lakuma
May 16 '07 #1
2 2410
frozenmist
179 Expert 100+
Hi lakuma,
See what you have to have is a dynamic SQL to create a table.
So You can have a procedure that executes the dynamic sql to create a table with name as that of a value that has been inserted. then you can call the procedure from the trigger.
I will give you a sample
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE SAMPLE_TABLE
  2. (
  3. NAME  VARCHAR(30),
  4. PLACE VARCHAR(30),
  5. ANIMAL VARCHAR(30),
  6. THING VARCHAR(30)
  7. )
  8.  
IS YOUR TABLE.

THIS IS THE PROCEDURE YOU CAN WRITE(creates a table with one column)
Expand|Select|Wrap|Line Numbers
  1. create procedure sample(IN NAME VARCHAR(100))
  2. language sql
  3. begin
  4. DECLARE V_SQL VARCHAR(500);
  5. SET V_SQL = 'CREATE TABLE '||NAME||' (COL1 INTEGER) ';
  6. EXECUTE IMMEDIATE V_SQL;
  7. end
  8.  
Finally, this can be your trigger

Expand|Select|Wrap|Line Numbers
  1. CREATE TRIGGER CREATE_TR 
  2. AFTER INSERT ON SAMPLE_TABLE
  3. REFERENCING NEW AS N
  4. FOR EACH ROW
  5. BEGIN ATOMIC
  6. CALL SAMPLE(N.NAME);
  7. END
  8.  
  9.  
Try something of this sort out on the table you have.
Hope that helped
Cheers
May 17 '07 #2
lakuma
2
Thank you so much...that helped a lot!!!
May 18 '07 #3

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

Similar topics

2
by: E.Keller | last post by:
Hello! We are developping a project using MS-SQLServer 7 and we need a process for the synchronization of 3 tables together. Inserts and updates in the table A should immediately and...
4
by: SAEED BASUDAN | last post by:
Hi i have 2 Table first one : Customer with 4 Fields : cst_no,cst_name,total_Debit,tot_credit second one : Transaction with 5 Fields : Trns_no,Trns_Date,cst_no,debit,credit MY QUESTION:...
9
by: Lauren Quantrell | last post by:
Is there a way to create a text file (such as a Windows Notepad file) by using a trigger on a table? What I want to do is to send a row of information to a table where the table: tblFileData has...
1
by: efinney | last post by:
Hi, I'm a newbie to sql server and this may be a really dumb question for some you. I'm trying to find some examples of sql server triggers that will set columns (e.g. the created and modified...
1
by: Ling Xiaoyu | last post by:
Hello there. Can anybody help me with Postgresql triggers? what I need is a trigger which update value of field "tables_rows.total_rows" to rows count of table "zzz" if I insert new row in table...
6
by: Chris Travers | last post by:
Hi all; I am using PostgreSQL 7.4 on RedHat 9, though I don't think that is important to this problem. I am attempting to write a rule that will check to see whether certain conditions are...
3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
8
by: Bri | last post by:
Greetings, I'm having a very strange problem in an AC97 MDB with ODBC Linked tables to SQL Server 7. The table has an Identity field and a Timestamp field. The problem is that when a new record...
15
by: Hi5 | last post by:
Hi, I am designing a database for a client in which It has a client table including the followings: 1-Table Client 2-Table lookupcategory 3-Table Ctegory
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
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: 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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.