在这个例子中,WITH 语句做了什么?我正在尝试随机生成数据

发布于 2024-08-04 17:31:22 字数 285 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人疚 2024-08-11 17:31:22

WITH 语法与使用本地临时表或内联视图相同。据我所知,它仅在 SQL Server(2005+,称为通用表表达式)和 Oracle(9i+,称为子查询分解)中受支持。预期用途是创建在单个查询中多次使用(即连接)的基本视图。

这是一个典型的示例:

WITH example AS (
     SELECT q.question_id,
            t.tag_name
       FROM QUESTIONS q
       JOIN QUESTION_TAG_XREF qtf ON qtf.question_id = t.question_id
       JOIN TAGS t ON t.tag_id = qtf.tag_id)
SELECT t.title,
       e1.tag_name
  FROM QUESTIONS t
  JOIN example e1 ON e1.question_id = t.question_id

...如果您使用以下命令,它将返回相同的结果:

SELECT t.title,
       e1.tag_name
  FROM QUESTIONS t
  JOIN (SELECT q.question_id,
               t.tag_name
          FROM QUESTIONS q
          JOIN QUESTION_TAG_XREF qtf ON qtf.question_id = t.question_id
          JOIN TAGS t ON t.tag_id = qtf.tag_id) e1 ON e1.question_id = t.question_id

您提供的示例:

WITH fileUIDS(fileUID) AS ( 
     VALUES(1) 
     UNION ALL
     SELECT t.fileUID+1 
       FROM fileUIDS t
      WHERE t.fileUID < 1000 )
INSERT INTO files 
    (fileUID, filename)
SELECT f.fileUID,
       TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdefgHij', '1234567890' )
  FROM fileUIDS f;

...是一个递归示例。它从 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:

WITH example AS (
     SELECT q.question_id,
            t.tag_name
       FROM QUESTIONS q
       JOIN QUESTION_TAG_XREF qtf ON qtf.question_id = t.question_id
       JOIN TAGS t ON t.tag_id = qtf.tag_id)
SELECT t.title,
       e1.tag_name
  FROM QUESTIONS t
  JOIN example e1 ON e1.question_id = t.question_id

...which will return identical results if you use:

SELECT t.title,
       e1.tag_name
  FROM QUESTIONS t
  JOIN (SELECT q.question_id,
               t.tag_name
          FROM QUESTIONS q
          JOIN QUESTION_TAG_XREF qtf ON qtf.question_id = t.question_id
          JOIN TAGS t ON t.tag_id = qtf.tag_id) e1 ON e1.question_id = t.question_id

The example you provided:

WITH fileUIDS(fileUID) AS ( 
     VALUES(1) 
     UNION ALL
     SELECT t.fileUID+1 
       FROM fileUIDS t
      WHERE t.fileUID < 1000 )
INSERT INTO files 
    (fileUID, filename)
SELECT f.fileUID,
       TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdefgHij', '1234567890' )
  FROM fileUIDS f;

...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).

瑕疵 2024-08-11 17:31:22
WITH x AS (...)

这将获取 ... 的输出并将其暂时视为名为 x 的表。

WITH x AS (...)
SELECT * FROM x

该语句本质上将为您提供与 ... 输出完全相同的内容,但它将被引用为表 x

WITH x AS (...)

This will take the output of the ... and treat it as a table named x, temporarily.

WITH x AS (...)
SELECT * FROM x

This statement will essentially give you the exact same thing as the ... outputs but it will instead be referenced as the table x

小鸟爱天空丶 2024-08-11 17:31:22

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.

∝单色的世界 2024-08-11 17:31:22

它正在创建 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文