SQL Update Select combined statement and application

Source: Internet
Author: User
Tags db2

Source: http://www.aimks.com/sql-update-the-select-statement-application.html

QL Update SELECT statement

The most common update syntax is:

UPDATE table_name SET = = VALUE

If my update value is taken out of a SELECT statement, and there are many columns, this syntax can be cumbersome.

First, to select out on a temporary variable, there are a lot of difficult to save.
Second, assign the variable to the value.

It's a lot of trouble to make a list, can you insert the result of the entire SELECT statement like insert?
Just like the following::

INSERT  into table1 (c1, C2, C3) (SELECT from Table2)

The answer is yes, the specific syntax is as follows:

UPDATE table1 alias SET = (SELECT  (column_name, column_name) from table2WHERE=  alias.column_name)WHERE= VALUE

Here's an example:
Two tables A, B, to make the Memo field value in B equal to the name value of the corresponding ID in table a
Table A:

ID     1       23          Sheets

Table B:

1234
ID    ClientName   1 2 3

(MS SQL Server) statement:

UPDATE b   SET   ClientName    = a.name from a     b    WHERE = b.ID

(ORALCE) Statement:

UPDATE b   SET   (ClientName)    =   (SELECTfromWHERE  = a.id)

Update set FROM statement format
When both where and set need to correlate a table for querying, the entire update executes, requiring two scans of the associated table, which is obviously less efficient.

For this scenario, the workaround for Sybase and SQL Server is to use the update ... SET ... From ... WHERE ... Syntax, which is actually getting updated data from the source table.

In SQL, table joins (left joins, right joins, Inner joins, and so on) are often used in SELECT statements.
In fact, in SQL syntax, these connections can also be used for UPDATE and DELETE statements, and the use of joins in these statements often results in a multiplier effect.

UPDATE SET = B.l_tuserid  from  Left JOIN T_productinfo on    b.l_id=A.productid

Used to synchronize data from two tables!

Syntax supported by both ORALCE and DB2:

UPDATE SET = (SELECTfromWHERE= b.id)

MS SQL Server does not support such a syntax, the corresponding wording is:

UPDATE A  SET= = = B3 from the left  JOIN on = b.ID

Personal feeling MS SQL Server's update syntax is more powerful. MS SQL Server notation:

UPDATE SET = = =  from WHERE = b.ID

The syntax for Oracle and DB2 is more cumbersome, as follows:

UPDATE SET = (SELECTfromWHERE=  b.id) WHERE inch (SELECTfromWHERE= b.id)

SQL Update Select combined statement and application

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.