SQL Server T-SQL view transactions

Source: Internet
Author: User

A view

Views are SQL statements that are stored in queries in the database.

A view is a table that is exported from one or more tables or views, and is a virtual table that can only be queried on the views and cannot be added, deleted, or changed.

Modify the view to be modified in the corresponding base table, and the modifications will automatically react to the views

To create a view:

Create View view name

as

SQL query statements--duplicate columns cannot appear in a view

Usage of the View:

SELECT * FROM view name

Two-business

Guarantee the complete implementation of the process, like the bank to take money, first in your account to deduct money, and then deposited in other people's account, but from your account to deduct money, suddenly the network broke, the other party did not receive the money, then your money is not, and other people's money did not add, business in order to prevent such a situation.

Format

BEGIN Tran--where the process begins

----------

If @ @error > 0

Begin

Rollback TRAN--rollback TRANSACTION, to begin TRAN position, as never happened

End

Else

Begin

Commit Tran-Commit a transaction, no problem, then just commit

End

Examples

BEGIN Tran--Open Transaction Declare @tran_errorint; --Storage ErrorSet@tran_error =0; --default no error update FruitSetNumbers = numbers-1 whereids='k002'Set@tran_error = @tran_error +@ @ERROR; update LoginSetaccount=account-1 whereUsername='Wangwu'Set@tran_error = @tran_error +@ @ERROR; insert into Orders values ('d002','Wangwu','2016-8-7')Set@tran_error = @tran_error +@ @ERROR; insert into OrderDetails values ('d002','k002',Ten)Set@tran_error = @tran_error +@ @ERROR;if@tran_error >0begin rollback Tran--Roll back the transaction, to the position of Begin Tran, when it never happened. EndElseBegin commit Tran--commit the transaction, no problem, then just commit to submit end
View Code

SQL Server T-SQL view transactions

Related Article

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.