magic table in sql server

Upload: amoramadi

Post on 04-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Magic Table in SQL Server

    1/3

    Sou rce : M ind cracker Netw ork (www.c-sharpcorner.com ) Print

    ARTICLE

    Introduction

    Tod ay we are going to play around with t he M agic Table in SQL Server. W hen we perform an insert or up dat e operationon any table then t he data is put into tables of in serted and d eleted d ata. These inserted and up dat ed t ables are know n

    as M agic Tables or Virtu al Table.

    I assum e you have basic kno wledg e of Sto red Procedures. For m ore help visit, Triggers in SQL Server.

    First of all we create a table nam ed emp

    Creation of table:

    createtableem p (empId int , empNamevarchar(15))

    Insertion of data:

    insert into em p

    select 1,'d 'union al l

    select 2,'e'union al l

    select 3,'e'union al l

    select 4,'p 'union al l

    select 5,'a'union al l

    select 6,'k'

    Output:

    select * from em p

    M agic Table:

    These are Virtual Tabl es nam ed inserted and upd ated t hat are created b y SQL Server when we perfo rm an insert, delete

    or u pd ate op eratio n. We can't see the M agic Table directly, We use a trigg er to see the Mag ic Table.

    c Table in SQL Server 2012 http://www.c-sharpcorner.com/UploadFile/63f5c2/magic-table-in-sq

    12/20/2012

  • 7/30/2019 Magic Table in SQL Server

    2/3

    Type of M agic Table:

    Inserted1.Deleted2.

    M agic Table for insert operation:

    W hen we insert d ata into a table th en data is first in serted in t he inserted t able.

    W e can use the follow ing t rigg er to see the inserted M agic Table.

    createt r iggermajic_insert

    o nem p

    fo rinsert

    as

    select * from inserted

    Output:

    insert into em p values(11,'d ' )

    W hen we execute that com mand , the t rigg er magic_insert fires and shows the table inserted.

    M agic Table for delete operation:

    W hen we delete data from any table then data is put into the deleted table.

    By using follo wing trig ger we can see the deleted M agic Table.

    createt r iggermagic_delete

    o nem p

    fo rdelete

    as

    select * from deleted

    Output:

    deletef rom em p whereempId= 11

    W hen we execute th at com mand the t rigg er magic_delete fires and shows the table deleted.

    c Table in SQL Server 2012 http://www.c-sharpcorner.com/UploadFile/63f5c2/magic-table-in-sq

    12/20/2012

  • 7/30/2019 Magic Table in SQL Server

    3/3

    M agic Table for update operation:

    W hen we perfo rm an upd ate operatio n then data from the first t able is deleted first, then after that the new upd ated

    dat a is insert ed.

    By using follo wing trig ger we can see the inserted and d eleted M agic Table.

    createt r iggermagic_update

    o nem p

    fo rupdate

    as

    select * from inserted

    select * from deleted

    Output:

    updateem p set empName= 'deepak' whereempId = 1

    W hen we execute th at com mand the t rigg er magic_upd ate fires and shows the tables inserted and d eleted.

    Summary:

    In th is article I described M agic Table in SQL Server. I hop e this article has helped y ou t o un derstand thi s to p ic. Please

    share if yo u kno w m ore abo ut t his. Your feedb ack and co nstruct ive cont ribut ions are welcom e.

    Thank you for using M indcracker Netw ork

    c Table in SQL Server 2012 http://www.c-sharpcorner.com/UploadFile/63f5c2/magic-table-in-sq

    12/20/2012