Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Insert Trigger

Author  Topic 

mase2hot
Starting Member

36 Posts

Posted - 2010-04-24 : 05:37:17
Hi,

I have 2 tables 1 is clients other is progress of their order. I want it so that when I add new clients into the cleints table, currently just copy and paste the date from excel. I need the Cleint ID (primary Key) to appear in the progress table. I have been looking online for a bit an it seems a trigger is best option? But I'm unable to get it working, my code is below....

CREATE TRIGGER PROGRESS_PK
ON dbo.Clients
AFTER INSERT
AS
BEGIN
Insert into dbo.Progress (ID)
Select ID
From dbo.clients
END

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-04-24 : 06:10:31
CREATE TRIGGER PROGRESS_PK
ON dbo.Clients
AFTER INSERT
AS
BEGIN
Insert into dbo.Progress (ID)
Select ID
From inserted
END
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-04-24 : 07:01:14
I think that even OUTPUT operator can be used for this situation.

I am here to learn from Masters and help new bees in learning.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-24 : 07:24:38
quote:
Originally posted by pk_bohra

I think that even OUTPUT operator can be used for this situation.

I am here to learn from Masters and help new bees in learning.


yup. but in that case you have to include at every place you do the insert to main table. if trigger is created you dont have to repeat the population logic for progress everywhere as once created it would be executed each time automatically for insertion.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mase2hot
Starting Member

36 Posts

Posted - 2010-04-24 : 08:08:23
Thanks, works a treat I was so close!...lol
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-04-24 : 11:14:55
quote:
Originally posted by visakh16

quote:
Originally posted by pk_bohra

I think that even OUTPUT operator can be used for this situation.

I am here to learn from Masters and help new bees in learning.


yup. but in that case you have to include at every place you do the insert to main table. if trigger is created you dont have to repeat the population logic for progress everywhere as once created it would be executed each time automatically for insertion.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/






Thanks visakh. Now I have one more reason to prefer trigger over output operator (I remember that again it depends on requirement).




I am here to learn from Masters and help new bees in learning.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-24 : 11:16:11
yeah...that really depends on requirement.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -