SQL output clause usage

Source: Internet
Author: User

The above is the original content of the table soloreztest, which is operated by the output clause.

A. Use output into for simple insert statements.

The following example inserts rows into the soloreztest table and usesOUTPUTClause returns the result of the statement@mytable Table variable
Declare @ mytable table
(
Id int identity (1, 1) primary key,
Name varchar (50)
)
Insert into soloreztest output inserted. Name into @ mytable values ('20140901 ')
Select * From soloreztest
Select * From @ mytable

The result is:

The output into clause is the insert @ mytable Table variable to synchronize data in the soloreztest table.
The output clause is only used to display the changed data inserted or deleted prefix.
Inserted Prefix: used to retrieve data in a newly inserted Table or updated data. The data available in the insert and update statements cannot appear in the delete statement.
Ed Prefix: used to retrieve deleted or updated data. Available and delete and update statements cannot appear in insert statements.

B. Use output in the delete statement
The following example shows how to delete the soloreztest row in the table by putting the information of the deleted row back.
Delete soloreztest output deleted. * Where id = 2

Similarly, you can use the output into statement to insert the deleted information to a new table.

C. Use output for update
The following instances update data in the table by using output to return the data before and after modification.
Update soloreztest set name = 'zz 'output inserted. Name, deleted. name where id = 3

Inserted. Name: indicates the updated data in the soloreztest table.
Deleted. Name: indicates the data before the update in the soloreztest table.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.