Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Towards the innovative SQL ideas
Browse by Tags · View All
ms sql server 119
ms sql 118
sql server 116
sql 115
database 102
tsql 81
#SQL Server 78
t-sql 75
#sql 71
sql server general 67

Archive · View All
April 2011 14
July 2011 12
May 2011 12
August 2011 11
June 2011 10
September 2011 8
December 2011 6
November 2011 6
June 2013 5
April 2013 5

OUTPUT clause with Insert, Update and Delete statements in SQL Server 2005

Jun 20 2011 12:00AM by Paresh Prajapati   

Sometime we are using triggers to get old and new values or new values for the historical data to save in another table. So we can get inserted, updated and deleted data using triggers.

But one amazing feature "Output" clause. This clause we are mostly use with stored procedures to get output variables. Output clause also can be used with insert, update and delete statements. I have never used it before. I don't want to miss the chance to use it right now.

Here i want to more brief you with some of the examples and small demo.

Creating database and tables.
-- Creating database
CREATE DATABASE OutputDB

GO

USE OutputDB

GO

-- Creating table
IF( Object_id('OutputTable') > 0 )
  DROP TABLE OutputTable

GO

CREATE TABLE OutputTable
  (
     Id    INT,
     Value VARCHAR(10)
  )

GO
 Output clause with Insert statement,
-- Using Output clause with Insert statement.
INSERT INTO OutputTable
output      inserted.Id,
            inserted.Value
SELECT 1,
       '1-Inserted'

INSERT INTO OutputTable
output      inserted.Id,
            inserted.Value
SELECT 2,
       '2-Inserted'

GO

 Output clause with Inserting records in another table,
-- Output clause with Insert records in another table
-- Creating table
IF( Object_id('InsertedTable') > 0 )
  DROP TABLE InsertedTable

GO

CREATE TABLE InsertedTable
  (
     Id    INT,
     Value VARCHAR(10)
  )

GO

INSERT INTO OutputTable
output      inserted.Id,
            inserted.Value
INTO InsertedTable
SELECT 3,
       '3-Inserted'

INSERT INTO OutputTable
output      inserted.Id,
            inserted.Value
INTO InsertedTable
SELECT 4,
       '4-Inserted'

GO

-- Let's see ther inserted values in InsertedTable
SELECT ID,
       value
FROM   OutputTable

GO

SELECT ID,
       value
FROM   InsertedTable

GO

 
Output clause with Update Statement,
-- Output clause with Update statement
-- Creating table
IF( Object_id('UpdatedTable') > 0 )
  DROP TABLE UpdatedTable

GO

CREATE TABLE UpdatedTable
  (
     Id    INT,
     Value VARCHAR(10)
  )

GO

-- Here we are insering new values in UpdatedTable table
UPDATE OutputTable
SET    value = '1-updated'
output inserted.Id,
       inserted.Value
INTO UpdatedTable
WHERE  Id = 1

-- Here we are inserting old values in UpdatedTable table
UPDATE OutputTable
SET    value = '2-updated'
output deleted.Id,
       deleted.Value
INTO UpdatedTable
WHERE  Id = 2

-- Let's check the records in UpdatedTable
SELECT ID,
       Value
FROM   OutputTable

GO

SELECT ID,
       Value
FROM   UpdatedTable

GO


Output clause with Delete statement,
-- Output clause with Delete statement
-- Creating table
IF( Object_id('DeletedTable') > 0 )
  DROP TABLE DeletedTable

GO

CREATE TABLE DeletedTable
  (
     Id    INT,
     Value VARCHAR(10)
  )

GO

-- Here we are insering old values in DeletedTable table
-- which are GOing to be delete
DELETE FROM OutputTable
output deleted.Id,
       deleted.Value
INTO DeletedTable
WHERE  Id = 3

-- Here we are inserting old values in UpdatedTable table
DELETE FROM OutputTable
output deleted.Id,
       deleted.Value
INTO DeletedTable
WHERE  Id = 4

-- Let's chek the records in UpdatedTable
SELECT ID,
       Value
FROM   OutputTable

GO

SELECT ID,
       Value
FROM   DeletedTable

GO

Get output in different way!

Tags: sql, sql server 2005, tsql, sql server, ms sql, ms sql server, t-sql, #SQL Server, mssql, database, sql server general, SQL Scripts, SQL new features,


Paresh Prajapati
6 · 22% · 7054
3
 
0
Lifesaver
 
 
0
Learned
 
0
Incorrect



Submit

1  Comments  

Your Comment


Sign Up or Login to post a comment.

"OUTPUT clause with Insert, Update and Delete statements in SQL Server 2005" rated 5 out of 5 by 3 readers
OUTPUT clause with Insert, Update and Delete statements in SQL Server 2005 , 5.0 out of 5 based on 3 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]