如何将数据集 ID 作为 FROM 语句中的变量指定。

huangapple go评论49阅读模式
英文:

How to specify dataset id as variable in FROM statement

问题

I'm here to provide the translation of the code snippet you provided. Here's the translated code:

DECLARE dataset_ids ARRAY<STRING> DEFAULT ["dataset_id_1","dataset_id_2"];

FOR dataset_id IN (
  SELECT * FROM UNNEST(dataset_ids) AS dataset_def
) DO

FOR table_row IN (
 SELECT * FROM CONCAT("project_id.", dataset_def, ".__TABLES__")
) DO
SELECT table_row.creation_time;
END FOR;
END FOR;

Please note that the code is now in Chinese, and the code-related content has been translated without any additional information or responses.

英文:
DECLARE dataset_ids ARRAY&lt;STRING&gt; DEFAULT [&quot;dataset_id_1&quot;,&quot;dataset_id_2&quot;];

FOR dataset_id IN (
  SELECT * FROM UNNEST(dataset_ids) AS dataset_def
) DO

FOR table_row IN (
 SELECT * FROM CONCAT(&quot;project_id.&quot;, dataset_id.dataset_def, &quot;.__TABLES__&quot;)
) DO
SELECT table_row.creation_time;
END FOR;
END FOR;

After executing this query, in which the dataset id is dynamically defined via a variable, the following error message is returned:

>Query error: Table-valued function not found: CONCAT at [5:16]

Query notes:

  • note 1: the query has been simplified as such that the focus is purely on the issue as outlined
  • note 2: "project_id", "dataset_id_1" and "dataset_id_2" are fictional in this simple example)

How (if possible in the first place) to successfully insert the dataset as a variable in the FROM statement in the query?

答案1

得分: 1

以下是已翻译的内容:

由于表名无法进行参数化,您应该考虑在如下脚本中使用动态 SQL。

-- `bigquery-public-data` 项目中的数据集
DECLARE dataset_ids DEFAULT ["austin_bikeshare", "austin_crime"];

-- 下面是用于存储查询结果的临时表。
CREATE TEMP TABLE result (table_id STRING, creation_time INT64);

FOR dataset_id IN (
  SELECT * FROM UNNEST(dataset_ids) AS dataset_def
) DO
  EXECUTE IMMEDIATE FORMAT('''
    INSERT INTO result
    SELECT table_id, creation_time FROM `bigquery-public-data.%s.__TABLES__`;
  ''',  dataset_id.dataset_def);
END FOR;

SELECT * FROM result;

-- 脚本结果
+--------------------+---------------+
|      table_id      | creation_time |
+--------------------+---------------+
| bikeshare_trips    | 1495684542045 |
| bikeshare_stations | 1495683814344 |
| crime              | 1496769664576 |
+--------------------+---------------+

另请参阅

英文:

Since a table name can't be parameterized, you should consider a dynamic sql in a script like below.

-- datasets in `bigquery-public-data` project
DECLARE dataset_ids DEFAULT [&quot;austin_bikeshare&quot;, &quot;austin_crime&quot;];

-- below is a temp table for storing query result.
CREATE TEMP TABLE result (table_id STRING, creation_time INT64);

FOR dataset_id IN (
  SELECT * FROM UNNEST(dataset_ids) AS dataset_def
) DO
  EXECUTE IMMEDIATE FORMAT(&quot;&quot;&quot;
    INSERT INTO result
    SELECT table_id, creation_time FROM `bigquery-public-data.%s.__TABLES__`;
  &quot;&quot;&quot;,  dataset_id.dataset_def);
END FOR;

SELECT * FROM result;

-- script result
+--------------------+---------------+
|      table_id      | creation_time |
+--------------------+---------------+
| bikeshare_trips    | 1495684542045 |
| bikeshare_stations | 1495683814344 |
| crime              | 1496769664576 |
+--------------------+---------------+

See also

huangapple
  • 本文由 发表于 2023年5月21日 20:03:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定