What is the WITH statement made in this example? I am trying to randomly generate data

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;
+3
source share
4 answers

WITH syntax is similar to using a local temp table or inline view. As far as I know, it is only supported in SQL Server (2005+, called Common Table Expressions) and Oracle (9i +, called factoring subquery). The intended use is to create a basic view that is used (i.e.: attached to) several times in a single request.

Here is 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 specified:

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 ( 1000, 0).

+13
WITH x AS (...)

... x, .

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

, ..., x

+3

WITH Common Table Expression (CTE). , "select fileUID,..." .

0

CTE (Common Table Expression). , , . .

http://4guysfromrolla.com/webtech/071906-1.shtml.

0
source

Source: https://habr.com/ru/post/1717484/


All Articles