GCP Bigquery - 联合查询中的查询参数

GCP Bigquery - query parameters in federated queries

我未能实施 proposed in this

我的sql代码:

declare bq_last_id string;
declare external_sql string;
set bq_last_id = 'select max(id) from bq_dataset.bq_table';
set external_sql = '"select * from mysql_table where id > ('|| bq_last_id ||')"';
execute immediate 'select * from external_query("my-gcp-project.my-region.my-connection-name",'|| external_sql || ');'

我收到以下错误

invalid table-valued function external_query Failed to get query schema from MySQL server. Error: MysqlErrorCode(1142): SELECT command denied to user 'XXX@'cloudsqlproxy~' for table 'XXX' at [1:22] at [1:1]

如果我像这样硬编码 bq_last_id 的值,它工作正常。

set external_sql = '"select * from mysql_table where id > 123456"';

编辑 1: 我在执行语句之前打印了语句,它看起来像这样,我想这对我来说很好。

select * from external_query("my-gcp-project.my-region.my-connection-name", "select * from mysql_table where id > (select max(id) from bq_dataset.bq_table)");

编辑 2: 最后我不得不插入数据,所以使用下面的答案和一个括号我能够这样做。

declare bq_last_id int64; # Change the type here
declare external_sql string;
set bq_last_id = (select max(id) from bq_dataset.bq_table); # put the query in parenthesis to force BQ to evaluate it
set external_sql = '"select * from mysql_table where id > ('|| bq_last_id ||')"';
execute immediate 'insert into bq_dataset.bq_table (select * from external_query("my-gcp-project.my-region.my-connection-name",'|| external_sql || '))'

谢谢。

感谢您的更新,这里的问题是:您在引用中使用 BQ table 查询您的 Cloud SQL 实例。那是行不通的。您必须先评估 max(id) ,然后再将其添加到您的查询中,就像那样

declare bq_last_id int64; # Change the type here
declare external_sql string;
set bq_last_id = (select max(id) from bq_dataset.bq_table); # put the query in parenthesis to force BQ to evaluate it
set external_sql = '"select * from mysql_table where id > ('|| bq_last_id ||')"';
execute immediate 'select * from external_query("my-gcp-project.my-region.my-connection-name",'|| external_sql || ');'