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
 Trigger to update in Foreign key

Author  Topic 

merwyn007
Starting Member

4 Posts

Posted - 2009-03-30 : 14:06:00
hi im new to sql i just want the syntax for trigger in which whenever a data is added in parent table automatically that data is entered through this trigger.

pls help as i will be implementing in project soon.

merwyn

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-03-30 : 14:52:45
Use the CREATE TRIGGER example syntax as seen in Books Online, and here is specifically what you need for the INSERT statement:

INSERT INTO SomeTable (...)
SELECT ... FROM inserted

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

merwyn007
Starting Member

4 Posts

Posted - 2009-03-31 : 00:57:54
hi Sir i want to add the data through trigger in child table. i want the syntax for trigger, as for this query u gave me Sir is that i have to run this query everytime when i insert data in parent table.

pls help Sir

merwyn
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-03-31 : 15:35:50
You don't seem to understand what I am saying. Wrap what I posted into a trigger, which you can find the syntax for in SQL Server Books Online. Check the CREATE TRIGGER topic.

By the way, I'm not male. I just love when people assume males for IT people.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-04-01 : 01:38:57
Yes Read Books Online on Sql Server to Create a Trigger on Insert,,,in This When you Insert a New Value in Ur Parent Table then this trigger fire automatically.. n update the value in your child table also,,,

something like
CREATE TRIGGER YourTriggerName
ON YourTable
AFTER INSERT
AS
BEGIN
UPDATE t
SET t.yourfield=(SELECT yourfield FROM Table)
FROM Table t
INNER JOIN INSERTED i
ON i.PK=t.PK
END

Pk Is Primary Key Of your Table,,,,
Go to Top of Page

merwyn007
Starting Member

4 Posts

Posted - 2009-04-01 : 05:52:00
hi this the query for update

I want to create a trigger such that when I insert a item it should insert one row in another table with same item code


merwyn
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-04-01 : 06:06:26
Sorry Then Use This As Above TAra Stated ,,,,

CREATE TRIGGER YourTriggerName
ON YourTable
AFTER INSERT
AS
BEGIN
INSERT INTO SomeTable (...)
SELECT ... FROM inserted
END

May BE like This,,,
Or Show us what will u try till now....
Thanks,,,,
Go to Top of Page

merwyn007
Starting Member

4 Posts

Posted - 2009-04-01 : 06:23:07
hi this my query

my table defination is
ItemEJBTable(itemCode int primary key,itemDescription varchar(50))
Vote_Info(itemCode int foreign key references ItemEJBTable , Number_Votes int)

CREATE TRIGGER TItemCode
ON ItemEJBTable
AFTER INSERT
AS
BEGIN
INSERT INTO Vote_Info(itemCode,0)
SELECT itemCode FROM inserted
END

now how to solve this im in great trouble now

merwyn
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-04-01 : 07:00:18
it works perfectly fine for me...

CREATE TRIGGER [ashuaaa]
ON [dbo].[emp]
AFTER INSERT
AS
BEGIN

SET NOCOUNT ON;
INSERT INTO emp1(sal)
SELECT sal FROM inserted
END
GO
Go to Top of Page
   

- Advertisement -