SQL executes and returns pre/post-execution results

Source: Internet
Author: User

1. Insert the data and return the inserted data:
INSERT into TESTTB (province,city) output inserted. Province, inserted. City VALUES (' Guangdong ', ' Shenzhen ')

2, similarly, the deletion of data is the same, but the use of deleted table.
Delete from TESTTB output deleted.* where id=1

3. Together: Returns both pre-and post-update data:
Update testtb SET province = ' Hunan ', city= ' Chenzhou ' OUTPUT ' I came from (pre-update) ' + DELETED. Province+deleted. City as [before], ' I'm from (updated) ' + inserted.province+inserted.city as [after] WHERE id=1

4. You can also save the returned results in a table variable, which is useful when deleting data and inserting deleted data into the history table
DECLARE @temp tabletable (Idint, Provincevarchar (), Cityvarchar () DELETEFROMTESTTB OUTPUT Deleted.*[em AIL Protected]>4select*[email protected]
--Returns the value before update ppdai_jr_shop.dbo.GeneratorUniqueNo SET suffix=suffix+2 OUTPUT inserted.suffix WHERE prefix= ' ppdtk '-- Returns the updated value of update ppdai_jr_shop.dbo.GeneratorUniqueNo SET suffix=suffix+2 OUTPUT deleted.suffix WHERE prefix= ' PPDTK '

SQL executes and returns pre/post-execution results

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.