OUTPUT Clause transfers the records affected by the currently executed INSERT, UPDATE, or DELETE operations to the specified table. So we can easily capture newly created or updated or deleted records. The OUTPUT statement reads the newly inserted records from the INSERTED table, deleted or updated records from the DELETED table. These tables are the virtual tables that are created at that time.
Create the table which we will update and insert some records.
CREATE TABLE [dbo].[Customer](
[CustomerId] [int] NULL,
[City] [varchar](50) NULL,
[Telephone] [varchar](50) NULL
) ON [PRIMARY]
INSERT INTO [dbo].[Customer](CustomerId,City,Telephone)
SELECT * FROM [dbo].[Customer]
Create the table which we will transfer the changes. Update the Telephone column where the City value is ‘İstanbul ‘ in the table and transfer the affected values to table which we have created named ChangesTable. Lastly, Select the ChangesTable.
DECLARE @ChangesTable AS TABLE (CustomerId int, OldTelephone char(50),NewTelephone char(50))
OUTPUT INSERTED.CustomerId, DELETED.Telephone, INSERTED.Telephone INTO @ChangesTable
SELECT * FROM @ChangesTable
DECLARE @DeletedTable AS TABLE (CustomerId int)
OUTPUT DELETED.CustomerId INTO @DeletedTable
SELECT * FROM @DeletedTable