Thursday, July 21, 2016

Magic Tables in Stored Procedure

We have seen that  how we can use inserted and deleted magic  table in triggers, now we  will learn  about   magic  tables in  stored procedures.

-- declare @InsertOutput1 table variable
DECLARE @InsertOutput1 table
(
  ID int,
  Title nvarchar(50),
  ModifiedDate datetime
);

-- insert new row into Post table
INSERT INTO Post
OUTPUT INSERTED.*
  INTO @InsertOutput1
VALUES(101, 'One Hundred Years of Solitude', GETDATE());

-- view inserted row in Posttable
SELECT * FROM Post;

-- view output row in @InsertOutput1 variable
SELECT * FROM @InsertOutput1;

In above example  output is nothing  but to get records from inserted magic table, in same  way we can use deleted magic table.

No comments:

Post a Comment