使用双 FROM 子句删除
Delete with double FROM clause
我很惊讶这能奏效。我不确定我明白为什么。
create table #tempt(something int)
DELETE FROM #tempt -- works fine
FROM #tempt EM
我希望需要在 DELETE 中使用别名,而且我特别希望首先不需要那个 FROM
。为什么第一个FROM
可以存在?
DELETE EM --what I would expect to work
FROM #tempt EM
因为如果您查看文档 msdn:
[ WITH <common_table_expression> [ ,...n ] ]
DELETE
[ TOP (expression ) [ PERCENT ] ]
[ FROM ]
{ { table_alias
| <object>
| rowset_function_limited
[ WITH (table_hint_limited [ ...n ] ) ] }
| @table_variable
}
[ <OUTPUT Clause> ]
[ FROM table_source [ ,...n ] ]
[ WHERE { <search_condition>
| { [ CURRENT OF
{ { [ GLOBAL ] cursor_name }
| cursor_variable_name
}
]
}
}
]
[ OPTION ( <Query Hint> [ ,...n ] ) ]
[; ]
<object> ::=
{
[ server_name.database_name.schema_name.
| database_name. [ schema_name ] .
| schema_name.
]
table_or_view_name
}
有两个FROM
!
第一个是可选的,用于标识目标 table。
第二个(不是标准的)它可用于通过连接而不是存在对目标 table 实现额外过滤。
经过进一步调查,第一个 FROM
似乎是编写 DELETE FROM
语句的标准方式。但是使用第二个 FROM
会使语句看起来不自然(如您所指出的),因此这可能是它是可选的背后的原因。
来自 MSDN(摘录):
DELETE
[ FROM ]
{ { table_alias
| table_or_view_name
}
[ FROM table_source [ ,...n ] ]
第一个FROM
:
FROM: An optional keyword that can be used between the DELETE
keyword and the target table_or_view_name
第一个 FROM
的参数:
table_alias: The alias specified in the FROM table_source clause
representing the table or view from which the rows are to be deleted.
table_or view_name: The name of the table or view from which the rows
are to be removed.
第二个FROM
:
FROM table_source: Specifies an additional FROM clause. This
Transact-SQL extension to DELETE allows specifying data from
and deleting the corresponding rows from the table in
the first FROM clause.
所以,简而言之:
- 第一个
FROM
是 optional 并且用于指定 target table 从哪个行开始被删除。您可以省略它,正如您在 OP 中指出的那样,只需使用第二个 FROM
中指定的 table_alias。
- 第二个
FROM
是一个T-SQL扩展,用来指定要从target中删除的行table.
我很惊讶这能奏效。我不确定我明白为什么。
create table #tempt(something int)
DELETE FROM #tempt -- works fine
FROM #tempt EM
我希望需要在 DELETE 中使用别名,而且我特别希望首先不需要那个 FROM
。为什么第一个FROM
可以存在?
DELETE EM --what I would expect to work
FROM #tempt EM
因为如果您查看文档 msdn:
[ WITH <common_table_expression> [ ,...n ] ] DELETE [ TOP (expression ) [ PERCENT ] ] [ FROM ] { { table_alias | <object> | rowset_function_limited [ WITH (table_hint_limited [ ...n ] ) ] } | @table_variable } [ <OUTPUT Clause> ] [ FROM table_source [ ,...n ] ] [ WHERE { <search_condition> | { [ CURRENT OF { { [ GLOBAL ] cursor_name } | cursor_variable_name } ] } } ] [ OPTION ( <Query Hint> [ ,...n ] ) ] [; ] <object> ::= { [ server_name.database_name.schema_name. | database_name. [ schema_name ] . | schema_name. ] table_or_view_name }
有两个FROM
!
第一个是可选的,用于标识目标 table。
第二个(不是标准的)它可用于通过连接而不是存在对目标 table 实现额外过滤。
经过进一步调查,第一个 FROM
似乎是编写 DELETE FROM
语句的标准方式。但是使用第二个 FROM
会使语句看起来不自然(如您所指出的),因此这可能是它是可选的背后的原因。
来自 MSDN(摘录):
DELETE
[ FROM ]
{ { table_alias
| table_or_view_name
}
[ FROM table_source [ ,...n ] ]
第一个FROM
:
FROM: An optional keyword that can be used between the DELETE keyword and the target table_or_view_name
第一个 FROM
的参数:
table_alias: The alias specified in the FROM table_source clause representing the table or view from which the rows are to be deleted.
table_or view_name: The name of the table or view from which the rows are to be removed.
第二个FROM
:
FROM table_source: Specifies an additional FROM clause. This Transact-SQL extension to DELETE allows specifying data from and deleting the corresponding rows from the table in the first FROM clause.
所以,简而言之:
- 第一个
FROM
是 optional 并且用于指定 target table 从哪个行开始被删除。您可以省略它,正如您在 OP 中指出的那样,只需使用第二个FROM
中指定的 table_alias。 - 第二个
FROM
是一个T-SQL扩展,用来指定要从target中删除的行table.