GCP BigQuery-联邦查询中的查询参数
我无法实现此question中建议的solution。
我的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 || ');'
我收到以下错误
无效的表值函数EXTERNAL_QUERY无法获取查询 来自MySQL服务器的架构。错误:MySQL错误代码(1142):SELECT命令 在[1:22]拒绝给表‘XXX’的用户‘XXX@’CloudSQlProxy~‘ [1:1]
如果我像这样硬编码bq_last_id的值,它可以正常工作。
set external_sql = '"select * from mysql_table where id > 123456"';
编辑1:
我在EXECUTE语句之前打印了语句,它看起来像这样,我想这看起来很好。
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表查询您的云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 || ');'
相关文章