SQL Server All-in-once features

Source: Internet
Author: User

In Select The alias created in the list cannot be in Select Used in the clause executed before the clause. In fact, expression aliases cannot be used Select Other expressions in the list. This restriction is caused SQL Another unique feature (All-at-once Operation) . For example Select List, calculate the expressionThe logical order is irrelevant.And hasUncertainty:

Select C1 + 1 as E1, C2 + 1 as E2.

Therefore, the following expression is not supported:

Select C1 + 1 as E1, E1 + 1 as E2.

You can only Select Steps after list ( For example Order Procedure ) Column alias.

 

Understanding: simultaneous operations(All-at-once Operation)

In most programming environments, we exchange the values of two variables, usually using a temporary variable:

// Assume that two assigned values exist. Int Type Variable A , B

Int temp;

Temp =;A = B; B = temp;

Of course, this is just the usual practice. You can also use the logical operators:

A = a ^ B;

B = B ^;

A = B ^;

Or:B = a + (A = B) * 0;

HoweverSQLYou can use the following statement to exchange column values in:

Update DBO. T1 set C1 = c2, C2 = C1;

Assume that all operations occur at the same time. In factThe table is not modified before the update is complete. ( It can be understood that the records in the table are locked during the update process. ) After all the results are calculated, the source data in the existing table is replaced. .

Therefore, the following update statement:

Update DBO. T1 set C1 = C1 + (select max (C1) from DBO. T1 );

This update will affect T1 All rows in the table C1 When columns are added, the update starts. T1 The largest C1 Value. In this case, the records in the table are locked after the update starts, and the database processes and computes data in the background. After all data is calculated, all data is replaced at one time. So you don't have to worry about the biggest C1 The value changes with the operation. It can be understood that the operation happened instantly.

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.