Home All Groups Group Topic Archive Search About

pLEASE HELP:URGENT please

Author
12 May 2005 12:41 AM
nikila
I am new to sql server . I have to write a trigger to insert the data into
second table when there is any update in first table. Do I have to execute
the trigger every time by some command or it automatically fires when first
table gets updated?

Thanks in advance
Niki

Author
12 May 2005 12:47 AM
Tom Moreau
It automatically fires.  Here's a rough example:

create trigger tru_MyTable on MyTable
after update
as

if @@ROWCOUNT = 0
    return

insert MyOtherTable
select
    *
from
    inserted
go


--
   Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada
www.pinpub.com
..
"nikila" <niki***@yahoo.com> wrote in message
news:O%232LMtoVFHA.1384@TK2MSFTNGP09.phx.gbl...
I am new to sql server . I have to write a trigger to insert the data into
second table when there is any update in first table. Do I have to execute
the trigger every time by some command or it automatically fires when first
table gets updated?

Thanks in advance
Niki
Author
12 May 2005 4:15 AM
Chandra
Hi Nikila

The difference between Trigger and Stored Procedure is,
You need to execute a stored procedure explicitly, to run the stored procedure

There is no way where you can fire a Trigger from outside. The Trigger is
fired when an operation is performed on a table.

U need a "FOR UPDATE" trigger in your situation

Here is how you do it:

CREATE TRIGGER <TRIGGER_NAME>
ON <TABLE-1>
FOR UPDATE AS

INSERT INTO TABLE-2 SELECT * FROM TABLE-1

GO

For more information about triggers, you can refer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_create2_7eeq.asp


--
best Regards,
Chandra
http://chanduas.blogspot.com/
---------------------------------------



Show quote
"nikila" wrote:

>  I am new to sql server . I have to write a trigger to insert the data into
> second table when there is any update in first table. Do I have to execute
> the trigger every time by some command or it automatically fires when first
> table gets updated?
>
> Thanks in advance
> Niki
>
>
>
Author
12 May 2005 11:36 AM
Tom Moreau
Uh, this trigger would insert the entire contents of TABLE-1 into TABLE-2
every time TABLE-1 was updated.

--
   Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada
www.pinpub.com
..
"Chandra" <Chan***@discussions.microsoft.com> wrote in message
news:A4F71FF9-CD9F-45B7-A95F-C728E89B156D@microsoft.com...

Hi Nikila

The difference between Trigger and Stored Procedure is,
You need to execute a stored procedure explicitly, to run the stored
procedure

There is no way where you can fire a Trigger from outside. The Trigger is
fired when an operation is performed on a table.

U need a "FOR UPDATE" trigger in your situation

Here is how you do it:

CREATE TRIGGER <TRIGGER_NAME>
ON <TABLE-1>
FOR UPDATE AS

INSERT INTO TABLE-2 SELECT * FROM TABLE-1

GO

For more information about triggers, you can refer:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_create2_7eeq.asp


--
best Regards,
Chandra
http://chanduas.blogspot.com/
---------------------------------------



Show quote
"nikila" wrote:

>  I am new to sql server . I have to write a trigger to insert the data
> into
> second table when there is any update in first table. Do I have to execute
> the trigger every time by some command or it automatically fires when
> first
> table gets updated?
>
> Thanks in advance
> Niki
>
>
>

AddThis Social Bookmark Button