Database Simplified Headline Animator

Database Simplified

Tuesday 28 June 2011

Use Of Magic Tables While Updating Or Deleting Records

 

The Following Post Shows how Magic Tables Help In Creating Data Audit On Updation, Insertion and Deletion.

image

Lets Create Another Table with Similar Structure To Keep Audit Of Above Table

CREATE TABLE [dbo].[Testing_Audit](
    [Id] [int],
    [EmpId] [varchar](10),
    [EmpName] [varchar](20),
    [Gender] [char](1),
    [Designation] [varchar](20),
    [Department] [varchar](20), 
    [Salary] [money],
    [UpdatedOn] [datetime],
    [EncodedOn] [datetime]
) 

image

Now Lets See How Magic Tables Pushes Data To Audit Table. In The Following Update Query I Am Updating Name Of Employee To Sandeep Kr Verma From Sandep Kumar. In Typical Audit I should be able to get History of Records effecting by Update or Delete.
See the Following Example How History Is Maintained In Audit Table On Updating Record.

Update Testing  Set EmpName='Sandeep Kr. Verma' 
Output Deleted.*,GETDATE() into Testing_Audit
where Id=1

image

Look at the highlighted line above(Plays Major Role)  which fetches Data From “Deleted Magic Table” and Inserts Into Audit Table.

Select * From Testing;
Select * From Testing_Audit;

image

As You Can see Above Results From Testing And Testing_Audit Table. So these Magic Tables Can Be Used For Various Purpose.

See The Following Example For Delete Audit.

Delete From Testing
Output Deleted.*,GETDATE() Into Testing_Audit
where Id=2

image

Lets Check Our Audit Table

Select * From Testing;
Select * From Testing_Audit;

image

As You can See On Deleting Record From Testing Table Record Moved To Audit Table With the Help Of “Deleted” Magic Tables.

Surprised smile

2 comments:


  1. - Ta hy vọng ngươi sẽ làm đúng yêu cầu của ta.
    Thượng Quan Uyển Nhi nói với Mộng Tiêu Hồn,trong lòng đã bắt đầu thỏa hiệp.

    - Ngươi cũng biết, nếu ta hài lòng, ta sẽ cùng ngươi đi xem sư phụ của ngươi xem đã chết hay chưa.dongtam
    game mu
    cho thue phong tro
    http://nhatroso.com/
    nhac san cuc manh
    tổng đài tư vấn luật
    http://dichvu.tuvanphapluattructuyen.com/
    văn phòng luật
    tổng đài tư vấn pháp luật
    thành lập công ty
    http://we-cooking.com/
    chém gió
    trung tâm ngoại ngữ
    Mộng Tiêu Hồn chăm chú nhìn Thượng Quan Uyển Nhi, ánh mắt giống như là muốn ăn tươi nuốt sống Thượng Quan Uyển Nhi vậy.

    - Ta không biết tại sao ngươi lại thích nữ nhân, chẳng lẽ ngươi không phải nữ nhân sao?
    Thượng Quan Uyển Nhi khẽ nói với Mộng Tiêu Hồn.

    - Thích nữ nhân tốt hơn nhiều so với thích nam nhân, nữ nhân mới có hiểu rõ được nữ nhân, không phải sao?
    Mộng Tiêu Hồn khẽ nói, ngọc thủ nhẹ nhàng, rơi vào và vòng tay trắng của Thượng Quan Uyển Nhi.

    ReplyDelete