更改表中的現有數據
語法
UPDATE
{
table_name WITH ( < table_hint_limited > [
| view_name
| rowset_function_limited
}
SET
{ column_name = { expression | DEFAULT | NULL }
| @variable = expression
| @variable = column = expression } [
{ { [ FROM { < table_source > } [
[ WHERE
< search_condition > ] }
|
[ WHERE CURRENT OF
{ { [ GLOBAL ] cursor_name } | cursor_variable_name }
] }
[ OPTION ( < query_hint > [
< table_source > ::=
table_name [ [ AS ] table_alias ] [ WITH ( < table_hint > [
| view_name [ [ AS ] table_alias ]
| rowset_function [ [ AS ] table_alias ]
| derived_table [ AS ] table_alias [ ( column_alias [
| < joined_table >
< joined_table > ::=
< table_source > < join_type > < table_source > ON < search_condition >
| < table_source > CROSS JOIN < table_source >
| < joined_table >
< join_type > ::=
[ INNER | { { LEFT | RIGHT | FULL } [OUTER] } ]
[ < join_hint > ]
JOIN
< table_hint_limited > ::=
{ FASTFIRSTROW
| HOLDLOCK
| PAGLOCK
| READCOMMITTED
| REPEATABLEREAD
| ROWLOCK
| SERIALIZABLE
| TABLOCK
| TABLOCKX
| UPDLOCK
}
< table_hint > ::=
{ INDEX ( index_val [
| FASTFIRSTROW
| HOLDLOCK
| NOLOCK
| PAGLOCK
| READCOMMITTED
| READPAST
| READUNCOMMITTED
| REPEATABLEREAD
| ROWLOCK
| SERIALIZABLE
| TABLOCK
| TABLOCKX
| UPDLOCK
}
< query_hint > ::=
{ { HASH | ORDER } GROUP
| { CONCAT | HASH | MERGE } UNION
| {LOOP | MERGE | HASH } JOIN
| FAST number_rows
| FORCE ORDER
| MAXDOP
| ROBUST PLAN
| KEEP PLAN
}
參數
table_name
需要更新的表的名稱
WITH ( < table_hint_limited > [
指定目標表所允許的一個或多個表提示
view_name
要更新的視圖的名稱
rowset_function_limited
OPENQUERY 或 OPENROWSET 函數
SET
指定要更新的列或變量名稱的列表
column_name
含有要更改數據的列的名稱
如果指定了限定的列名稱
UPDATE authors
SET authors
WHERE au_fname =
FROM 子句中指定的表的別名不能作為 SET column_name 子句中的限定符使用
UPDATE titles
SET t
FROM titles t
WHERE t
AND s
若要使上例合法
UPDATE titles
SET ytd_sales = t
FROM titles t
WHERE t
AND s
expression
變量
DEFAULT
指定使用對列定義的默認值替換列中的現有值
@variable
已聲明的變量
SET @variable = column = expression 將變量設置為與列相同的值
FROM < table_source >
指定用表來為更新操作提供准則
table_name [[AS] table_alias ]
為更新操作提供准則的表的名稱
如果所更新表與 FROM 子句中的表相同
view_name [ [ AS ] table_alias ]
為更新操作提供准則的視圖的名稱
WITH ( < table_hint > [
為源表指定一個或多個表提示
rowset_function [ [AS] table_alias ]
任意行集函數的名稱和可選別名
derived_table
是從數據庫中檢索行的子查詢
column_alias
替換結果集內列名的可選別名
由兩個或更多表的積組成的結果集
SELECT *
FROM tab
RIGHT OUTER JOIN tab
ON tab
ON tab
對於多個 CROSS 聯接
指定聯接操作的類型
INNER
指定返回所有相匹配的行對
LEFT [OUTER]
指定除所有由內聯接返回的行外
RIGHT [OUTER]
指定除所有由內聯接返回的行外
FULL [OUTER]
如果來自左表或右表的某行與選擇准則不匹配
指定聯接提示或執行算法
JOIN
表示聯接所指定的表或視圖
ON
指定聯接所基於的條件
FROM Suppliers JOIN Products
ON (Suppliers
當條件指定列時
CROSS JOIN
指定兩個表的矢量積
WHERE
指定條件來限定所更新的行
搜索更新指定搜索條件來限定要刪除的行
定位更新使用 CURRENT OF 子句指定游標
為要更新行指定需滿足的條件
From:http://tw.wingwit.com/Article/os/xtgl/201311/8891.html