views:

94

answers:

2

I have sql server 2005 and need to create trigger for insert query.

I have Table naemd as "Log" with column named as UserID,UserName,LogDate,LogTime and want to transfer data into other table named as "DataTable" with same column name.

I have created trigger

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TRIGGER [dbo].[Transfer] on [dbo].[Log]
AFTER INSERT
AS
BEGIN

insert into DataTable (UserID,UserName,LogDate,LogTime)

SELECT UserID,UserName,LogDate,LogTime 
FROM Log where UserID not in(select UserID from DataTable)

END

New Data is updated on daily basis in "Log" table and so i want to transfer new data from Log table to DataTable with trigger.Execution time is very high and so no output .

A: 

In your trigger definition, you should be using the inserted table, which contains all the rows added to the Log Table.

Mitch Wheat
join is not needed in his case :)
silent
If he's simply looking to insert new rows period, as an audit log so to speak, there is no need for a join, just a straight SELECT ... FROM INSERTED .
eidylon
Thanks: updated.
Mitch Wheat
+2  A: 

you have "inserted" table in trigger, so you can insert data from it

insert into DataTable (UserID, UserName, LogDate, LogTime)
select UserID, UserName, LogDate, LogTime 
from inserted
silent
Yes, selecting from the base table will insert ALL rows EVERY time, not just NEW rows. This could EASILY explain long execution times. I'm willing to bet dollars to donuts you really need to change INSERT ... SELECT ... FROM LOG to INSERT ... SELECT ... FROM INSERTED .
eidylon