PDA

View Full Version : راهنمایی جهت نوشتن یک Trigger



nasr
شنبه 28 شهریور 1388, 16:07 عصر
سلام
من می خوام یک تریگر بنویسم که وقتی در Table1 عمل Insert یا Update انجام میشه در جدولی با نام LogTB رکوردی اضافه بشه و یکی از فیلدهای Table1 را بعلاوه عنوان Update یا Insert را در آن قرار بگیره

اگه کد زیر را نگاه کنید در جاهایی که *** گذاشتم نمیدونم چی بنویسم


Create trigger testLog
on Table1
after insert, update as
begin
declare @uid nvarchar(256)
set @uid = ***Table1 ID***
declare @UpOrIns nvarchar(256)
set @UpOrIns= ***Update Or Insert***
INSERT INTO LogTB
(TBName, Action, UserID)
VALUES (N'Table1', @UpOrIns,@Uid)
end

محمد سلیم آبادی
شنبه 28 شهریور 1388, 16:36 عصر
سلام،
برای بدست آوردن داده هایی که در جدول مورد اشاره شده ی trigger درج یا ویرایش شده اند می توانید از جدول مجازی inserted استفاده کنید:


Select @uid=ID From inserted


و برای مشخص کردن عمل درج یا ویرایش به نظر من باید دو trigger جدا برای این کار بنویسید که در یکی مقدار update و دیگری مقدار insert درج شود.

مثلا در update trigger داشته باشید:


set @UpOrIns= 'Update'

nasr
دوشنبه 30 شهریور 1388, 09:24 صبح
سلام
من این تریگر را نوشتم ولی مشکل اینجاست که با هر تغییر در جدول Alum دو رکورد به جدول Log اضافه میشه

یعنی ظاهرا در قسمت

select @uidD = code_karbar from deleted
select @uidI = code_karbar from insertedهر دو متغیر را مقدار دهی میکنه

این کد کل تریگر

alter trigger testLog
on alum
after insert, update,delete as
begin
declare @UpOrIns nvarchar(256)
declare @uidD nvarchar(256)
declare @uidI nvarchar(256)

set @uidD = ''
set @uidI = ''

select @uidD = code_karbar from deleted
select @uidI = code_karbar from inserted

درصورت Delete در جدول Alum
if @UidD <> ''
begin
set @UpOrIns= 'Del'
INSERT INTO LogTB
(TBName, Action, UserID)
VALUES (N'Table1', @UpOrIns,@UidD)
end

درصورت Insert در جدول Alum

if @UidI <> ''
begin
set @UpOrIns= 'Ins'
INSERT INTO LogTB
(TBName, Action, UserID)
VALUES (N'Table1', @UpOrIns,@UidI)
end

درصورت Update جدول Alum

if ((@UidI = '') and (@UidD='' ))
begin
set @UpOrIns= 'Upd'
INSERT INTO LogTB
(TBName, Action, UserID)
VALUES (N'Table1', @UpOrIns,@UidU)
end

end

محمد سلیم آبادی
دوشنبه 30 شهریور 1388, 11:13 صبح
وقتی که یک سطر از جدول Update می شود دو جدول مجازی Deleted و Inserted مقدار دهی می شوند. جدول Deleted داده های قبل از ویرایش و جدول Inserted داده های جدید را نگهداری می کنند.

از کد زیر استفاده کنید


alter trigger tes
on test
after delete, update, insert
as
begin
declare @UpOrIns nvarchar(256)
declare @uidD nvarchar(256)
declare @uidI nvarchar(256)

set @uidD = ''
set @uidI = ''

select @uidD=t from deleted
select @uidI=t from inserted

if @uidD<>'' and @uidI='' insert test2 values (null, null, 'd')--For Delete
if @uidI<>'' and @uidD='' insert test2 values ('i', null, null)-- For Insert
if ((@UidI <> '') and (@UidD <> '' )) insert test2 values (null, 'u', null)--For Update

end

truncate table test
insert test values('a')
insert test values('b')
insert test values('c')
insert test values('d')
update test set t='aa' where t='a'
--truncate table test2
delete test where t='b'
select * from test2
/*
i u d
---------- ---------- ----------
i NULL NULL
i NULL NULL
i NULL NULL
i NULL NULL
NULL u NULL
NULL NULL d
*/

PeymanPC
شنبه 07 اسفند 1389, 09:49 صبح
با سلام
من می خوام یک تریگر update که log جدول x رو ثبت کنه و مقادیر قبل از بروزرسانی و بعد از بروزرسانی را در جدول y ثبت کند. بنویسم که فیلد ds جدول x از نوع ntext می باشد. من کد زیر رو نوشتم مشکلم اینه که مقدار فیلد des قبل از بروزرسانی رو همان مقدار بعد تغییر قرار می دهد.لطفا راهنمایی کنید.
با تشکر


ALTER trigger [dbo].[testLog]
on [dbo].[X]
for insert, update as
begin
declare @title nvarchar(256)
declare @des nvarchar(MAX)
declare @UpOrIns nvarchar(256)
declare @uidD int
declare @uidI int


set @uidD = ''
set @uidI = ''

Select @uidI=t.id,@title=t.title,@des=t.des
from X t join Inserted on
t.id=Inserted.id

Select @uidD=t.id,@title=t.title,@des=t.des
from X t join deleted on
t.id=deleted.id


--درصورت Insert در جدول X

if ((@UidI <> '') and (@UidD='' ))
begin
set @UpOrIns= 'Ins'
INSERT INTO Y
(id, Title, des,Action)
VALUES (@uidI,@Title,@des, @UpOrIns)
end

--درصورت Update جدول X

if ((@UidI <> '') and (@UidD<>'' ))
begin

INSERT INTO Y
(id, Title,des, Action)

SELECT ID,Title,des ,'UpdBefore'
FROM deleted

set @UpOrIns= 'UpdAfter'

INSERT INTO Y
(id, Title, des,Action)
VALUES (@uidI,@Title,@des, @UpOrIns)

end

end

end