Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi i have 3 tables t1,t2,t3 in database. t1 contains fields(tid,taddres,tdate )and table t2 has fields(tiid,tcontcano).table t3 has two fields(toneid,twoeid).

table 3 both feilds(toneid,twoeid) are forging key depending upon tid(table t1) and tiid(table t2) respectively

now i want to create a trigger on table t1 and t2 such that whenever any data is inserted or deleted or updated in table t1 and t2 respectively then data gets inserted in table t3 .

second problem is how to get tid(t1) and (tiid) values inserted in table t3 through triggers

is it possible to create a single trigger which acts when a data get inserted in two tables
Posted

I think you should use transaction and insert a row in Table3 in a transaction.

pseudocode
begin transaction

insert into table1
get t1id

insert into table2
get t2id

insert into table3 with t1id and t2id

commit
 
Share this answer
 
I think your problem will not be solved by triggers.
You should bundle your operations into a StoredProcedure whether they are in a transaction or not.
Just pass the parameters to the StoredProcedure and do whatever you want with those three tables.
 
Share this answer
 
v2
Comments
Bala Selvanayagam 8-Nov-11 12:14pm    
5ed
As correctly said by Amir, you have to use stored procedure and here goes the sample implementation

1. Create t1

SQL
CREATE TABLE [dbo].[t1](
    [tid] [int] IDENTITY(1,1) NOT NULL,
    [taddress] [varchar](max) NULL,
    [tdate] [date] NULL,
 CONSTRAINT [PK_t1] PRIMARY KEY CLUSTERED
(
    [tid] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]



2.Create t2

SQL
CREATE TABLE [dbo].[t2](
    [tiid] [int] IDENTITY(1,1) NOT NULL,
    [tcontactno] [varchar](50) NULL,
 CONSTRAINT [PK_t2] PRIMARY KEY CLUSTERED
(
    [tiid] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]




3.Create t3

SQL
CREATE TABLE [dbo].[t3](
    [toneid] [int] NULL,
    [twoeid] [int] NULL
) ON [PRIMARY]



4.Create stored procedure

SQL
Create PROCEDURE spInsertContact
    @address varchar(max),@contactno varchar(max),@tdate date
AS
BEGIN
    SET NOCOUNT ON;
    Declare @toneId int,@twoeid int


    insert into t1(taddress,tdate) values (@address,@tdate)
    set @toneId=SCOPE_IDENTITY()


    insert into t2(tcontactno) values (@contactno);
    set @twoeid=SCOPE_IDENTITY()

    insert into t3(toneId,twoeid) values (@toneId,@twoeid)

END




5. You can sample execute the stored procedure

spInsertContact 'sample address1','sample contact 1','13 jun 1990'



hope this helps, i have tested in SQL 2008 and it works
 
Share this answer
 
Comments
Costica U 8-Nov-11 7:22am    
My 5
Bala Selvanayagam 8-Nov-11 12:13pm    
Thanks Costica
Amir Mahfoozi 8-Nov-11 7:27am    
+5
Bala Selvanayagam 8-Nov-11 12:14pm    
Thanks Amir,

My solution is based on your suggestion anyway

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900