Magic table for trigger in SQL Server
ใน SQL Server ในการ insert ,Update and delete ข้อมูลต่างๆที่เกิดขึ้นจะถูกเก็บไว้ใน magic table อยู่ 2 table คือ inserted,deleted tables inserted table จะเก็บข้อมูล recordที่ถูกดำเนินการ และจะเก็บข้อมุลหลังจากการ Update เสร็จสิ้น Deleted table จะเก็บข้อมูล record ที่มีการลบไปล่าสุดและเก็บข้อมุลเก่าก่อนการ Update Record ไว้ ในการเขียน Trigger สำหรับ table ใน SQL Server สำหรับการ Insert,delete จะไม่มีปัญหาอะไรดึงข้อมูล มาตรวจสอบจาก 2 table นั้นโดยตรง แต่ถ้าเป็นการ Update ก็จะต้องเลือกให้ดีว่าจะเอาข้อมูลอะไร ก่อนหรือหลังการ Update มาเก็บเป็น log นะครับ ตัวอย่างครับ CREATE TRIGGER [dbo].[LogTrigger] ON [dbo].[zzz] AFTER DELETE,UPDATE AS BEGIN SET NOCOUNT ON; if exists(select * from inserted) begin insert into log_OptFac select *,GETDATE(),’U’ from deleted end else begin insert into log_OptFac select *,GETDATE(),’D’ from deleted end END