简体   繁体   中英

OUTPUT Clause in Sql Server (Transact-SQL)

I Know that OUTPUT Clause can be used in INSERT, UPDATE, DELETE, or MERGE statement . The results of an OUTPUT clause in a INSERT, UPDATE, DELETE, or MERGE statements can be stored into a target table .

But, when i run this query

select   * from  <Tablename>  output

I didn't get any error. The query executed as like select * from tablename with out any error and with same no. of rows

So what is the exact use of output clause in select statement. If any then how it can be used? I searched for the answer but i couldn't find a answer!!

The query in your question is in the same category of errors as the following (that I have also seen on this site)

SELECT *
FROM   T1 NOLOCK 

SELECT *
FROM   T1
       LOOP JOIN T2
         ON X = Y 

The first one just ends up aliasing T1 AS NOLOCK. The correct syntax for the hint would be (NOLOCK) or ideally WITH(NOLOCK) .

The second one aliases T1 AS LOOP. To request a nested loops join the syntax would need to be INNER LOOP JOIN

Similarly in your question it just ends up applying the table alias of OUTPUT to your table.

None of OUTPUT, LOOP, NOLOCK are actually reversed keywords in TSQL so it is valid to use them as a table alias without needing to quote them, eg in square brackets.

OUTPUT clause return information about the rows affected by a statement. OUTPUT Clause is used along with INSERT , UPDATE , DELETE , or MERGE statements as you mentioned. The reason it is used is because these statements themselves just return the number of rows effected not the rows effected. Thus the usage of OUTPUT with INSERT , UPDATE , DELETE , or MERGE statements helps the user by returning actual rows effected.

SELECT statement itself returns the rows and SELECT doesn't effect any rows. Thus the usage of OUTPUT clause with SELECT is not required or supported. If you want to store the results of a SELECT statement into a target table use SELECT INTO or the standard INSERT along with the SELECT statement.

EDIT

I guess I misunderstood your question. AS @Martin Smith mentioned its is acting an alias in the SELECT statement you mentioned.

IF OBJECT_ID('tempdelete') IS NOT NULL DROP TABLE tempdelete
GO

IF OBJECT_ID('tempdb..#asd') IS NOT NULL DROP TABLE #asd
GO


CREATE TABLE tempdelete (
    name NVARCHAR(100)
)

INSERT INTO tempdelete VALUES ('a'),('b'),('c')

--Creating empty temp table with the same columns as tempdelete
SELECT * INTO #asd FROM tempdelete WHERE 1 = 0

DELETE FROM tempdelete 
OUTPUT deleted.* INTO #asd

SELECT * FROM #asd

This is how you can put all the deleted records in to a table. The problem with that is that you have to define the table with all the columns matching the table from which you are deleting. This is how i do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM