在这个例子中,WITH 语句做了什么?我正在尝试随机生成数据
INSERT INTO files (fileUID, filename)
WITH fileUIDS(fileUID) AS
( VALUES(1) UNION ALL
SELECT fileUID+1 FROM fileUIDS WHERE fileUID < 1000 )
SELECT fileUID,
TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdefgHij', '1234567890' )
FROM fileUIDS;
INSERT INTO files (fileUID, filename)
WITH fileUIDS(fileUID) AS
( VALUES(1) UNION ALL
SELECT fileUID+1 FROM fileUIDS WHERE fileUID < 1000 )
SELECT fileUID,
TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdefgHij', '1234567890' )
FROM fileUIDS;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
WITH 语法与使用本地临时表或内联视图相同。据我所知,它仅在 SQL Server(2005+,称为通用表表达式)和 Oracle(9i+,称为子查询分解)中受支持。预期用途是创建在单个查询中多次使用(即连接)的基本视图。
这是一个典型的示例:
...如果您使用以下命令,它将返回相同的结果:
您提供的示例:
...是一个递归示例。它从 1 开始,总共生成 999 个 fileuid(如果从 0 开始,则生成 1,000 个)。
The WITH syntax is the same as using either a local temp table or inline view. To my knowledge, it's only supported in SQL Server (2005+, called Common Table Expressions) and Oracle (9i+, called Subquery Factoring). The intended use is for creating a basic view that is used (ie: joined to) multiple times in a single query.
Here's a typical example:
...which will return identical results if you use:
The example you provided:
...is a recursive one. It's starting at 1, generating 999 fileuids in total (it would be 1,000 if it had started at 0).
这将获取
...
的输出并将其暂时视为名为x
的表。该语句本质上将为您提供与
...
输出完全相同的内容,但它将被引用为表x
This will take the output of the
...
and treat it as a table namedx
, temporarily.This statement will essentially give you the exact same thing as the
...
outputs but it will instead be referenced as the tablex
WITH 词用于创建公共表表达式 (CTE)。在本例中,它创建一个内联表,“select fileUID, ...”部分从中提取数据。
The WITH word is used to create a Common Table Expression (CTE). In this case, it's creating an inline table that the "select fileUID, ..." part is pulling data from.
它正在创建 CTE(通用表表达式)。这基本上是一个您不必创建、删除或声明的表。批处理运行后它将自动删除。
查看 https:// web.archive.org/web/20210927200924/http://www.4guysfromrolla.com/webtech/071906-1.shtml 了解更多信息。
It is creating CTE (Common Table Expression). This is a basically a table that you don't have to create, drop, or declare in anyway. It will be automatically deleted after the batch has ran.
Check out https://web.archive.org/web/20210927200924/http://www.4guysfromrolla.com/webtech/071906-1.shtml for more info.