Jump to content

Recommended Posts

Posted

I have a Table named Table_Lookup every week it was updated. So I have created 2 triggers on that table, if new records are inserted triggers fires and Inserts the New data in to another Table named as Table_Lookup_Change.
Note:I have data up to August in Table_Lookup but when trigger is fired it inserts data only up to July it is missing one month data.



First Trigger:

ALTER trigger [dbo].[trg_Table_Lookup]
On [dbo].[Table_Lookup]
after insert, update
as
begin
if (exists(select 1 from deleted))
begin
Insert into Table_Lookup_Change (MediaFactor_ID,Media, Year, Month, Factor, FLMNet, UpdatedDate, UpdatedBy, Filepath)
Select A.MediaFactor_ID, A.Media, A.Year, A.Month, A.Factor, A.FLMNet, SYSDATETIME(), suser_name(), A.FilePath
From deleted A Left join MediaFactor_Lookup_Change B
on A.MediaFactor_ID = B.MediaFactor_ID
and A.Media = B.Media
and A.Year = B.Year
and A.Month = B.Month
and A.Factor = B.Factor
and A.FLMNet = B.FLMNet
Where A.FLMNet is not null
and B.Media is null
and B.Year is null
and B.Month is null
and B.Factor is null
and B.FLMNet is null
end
else
begin
Insert into Table_Lookup_Change (MediaFactor_ID,Media, Year, Month, Factor, FLMNet, UpdatedDate, UpdatedBy, FilePath)
Select A.MediaFactor_ID, A.Media, A.Year, A.Month, A.Factor, A.FLMNet, SYSDATETIME(), suser_name(), A.FilePath
From inserted A Left join MediaFactor_Lookup_Change B
on A.MediaFactor_ID = B.MediaFactor_ID
and A.Media = B.Media
and A.Year = B.Year
and A.Month = B.Month
and A.Factor = B.Factor
and A.FLMNet = B.FLMNet
Where A.FLMNet is not null
and B.Media is null
and B.Year is null
and B.Month is null
and B.Factor is null
and B.FLMNet is null
and A.FLMNet is not null
end
end


Second Trigger:

ALTER trigger [dbo].[trg_Table_Lookup_SetLastModifiedDateAndUser]
on [dbo].[Table_Lookup]
after insert, update
as
begin
if (exists(select 1 from deleted))
begin
-- trigger is firing after update
update t
set t.LastModified = sysdatetime(),
t.LastModifiedBy = suser_name()
from dbo.Table_Lookup t
inner join inserted i on i.MediaFactor_ID = t.MediaFactor_ID
end
else
begin
-- trigger is firing after insert
update t
set t.Created = sysdatetime(),
t.CreatedBy = suser_name(),
t.LastModified = sysdatetime(),
t.LastModifiedBy = suser_name()
from dbo.Table_Lookup t
inner join inserted i on i.MediaFactor_ID = t.MediaFactor_ID
end
end

Posted

irrespective of TG \ SA .. please help [url="http://www.andhrafriends.com/user/25715-vuncle/"][img]http://www.andhrafriends.com/uploads/profile/photo-thumb-25715.jpg?_r=1361928836[/img][/url]

Posted

[img]https://lh4.googleusercontent.com/-Wz3-pV5v8wE/USWWW7b3ZSI/AAAAAAAANeM/1zDu5_5-lX4/s150/Brahmi-4.gif[/img]

Posted

[quote name='SQLbie' timestamp='1376420977' post='1304110386']
SQL vallu okkaru kuda lera :( :(
[/quote]
Unnaru bhayya....not at the moment

Posted

[code]
create table test_employee
(
Eid BIGINT,
Ename VARCHAR(20),
Esal BIGINT,
mdfy_dt DATETIME DEFAULT GETDATE(),
mdfy_by VARCHAR(30) DEFAULT SUSER_SNAME()
)
GO

create table test_employee_lookup
(
Eid BIGINT,
Ename VARCHAR(20),
Esal BIGINT,
mdfy_dt DATETIME DEFAULT GETDATE(),
mdfy_by VARCHAR(30) DEFAULT SUSER_SNAME()
)
GO

CREATE TRIGGER trg_insert_emp ON test_employee
AFTER INSERT
AS
BEGIN

INSERT INTO test_employee_lookup
SELECT Eid, Ename, Esal, mdfy_dt, mdfy_by
FROM inserted i
LEFT JOIN test_employee_lookup e ON i.eid = e.eid
WHERE e.eid IS NULL

END
GO

CREATE TRIGGER trg_update_emp ON test_employee
AFTER Update
AS
BEGIN

UPDATE t
SET Ename = i.Ename,
Esal = i.Esal,
mdfy_dt = i.mdfy_dt,
mdfy_by = i.mdfy_by
FROM test_employee_lookup t
INNER JOIN inserted i ON i.eid = t.eid

END
GO
[/code]

This is what I will do for my Triggers.. I don't understand Why you are using after update in an insert trigger until you have reason for so..

do an insert and update and see... This worked in my machine well...

×
×
  • Create New...