英文:
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<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_id.dataset_def, ".__TABLES__")
) 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 ["austin_bikeshare", "austin_crime"];
-- 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("""
INSERT INTO result
SELECT table_id, creation_time FROM `bigquery-public-data.%s.__TABLES__`;
""", 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论