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
 General SQL Server Forums
 New to SQL Server Programming
 Quick Trigger Logic Question

Author  Topic 

BlackenedCatfish
Starting Member

3 Posts

Posted - 2011-11-29 : 13:03:55
Hi,

I have the following trigger:



CREATE TRIGGER table_copy
ON MailOrderManager.dbo.FDX
FOR Insert
AS
BEGIN
INSERT INTO MailOrderManager.dbo.FDX_TRANSFER SELECT * FROM MailOrderManager.dbo.FDX
END
GO

I need to add an IF EXISTS condition that compares the value in column FDX_ID located in MailOrderManager.dbo.FDX to the same column name in MailOrderManager.dbo.FDX_TRANSFER.

How can I do that as easily as possible? I'm really new to SQL, only did some of it back in school years ago and need to make this little trigger work without creating a ton of dupes.

Thanks for any help!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-29 : 13:05:44
you should be using INSERTED table instead of MailOrderManager.dbo.FDX

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

Go to Top of Page

BlackenedCatfish
Starting Member

3 Posts

Posted - 2011-11-29 : 13:13:06
quote:
Originally posted by visakh16

you should be using INSERTED table instead of MailOrderManager.dbo.FDX

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




Hi Visakh,

Like I mentioned beforehand, I really don't know much SQL at all. Could you please explain what you mean?

Thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-29 : 13:16:05
[code]
CREATE TRIGGER table_copy
ON MailOrderManager.dbo.FDX
FOR Insert
AS
BEGIN
INSERT INTO MailOrderManager.dbo.FDX_TRANSFER SELECT * FROM INSERTED
END
GO
[/code]

this will make sure only newly inserted records gets inserted in other table as well

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

Go to Top of Page

BlackenedCatfish
Starting Member

3 Posts

Posted - 2011-11-29 : 13:52:49
quote:
Originally posted by visakh16


CREATE TRIGGER table_copy
ON MailOrderManager.dbo.FDX
FOR Insert
AS
BEGIN
INSERT INTO MailOrderManager.dbo.FDX_TRANSFER SELECT * FROM INSERTED
END
GO


this will make sure only newly inserted records gets inserted in other table as well

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




Excellent, thank you.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-30 : 01:33:59
wc

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

Go to Top of Page
   

- Advertisement -