【发布时间】:2022-01-12 11:13:16
【问题描述】:
我的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 服务器的架构。错误:MysqlErrorCode(1142):SELECT 命令 在 [1:22] 拒绝用户 'XXX@'cloudsqlproxy~' 访问表 'XXX' [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 || '))'
谢谢。
【问题讨论】:
-
您确定生成的查询吗?您是否尝试在立即执行之前打印它?
-
我添加了一个编辑来显示它在立即执行之前的样子。
标签: mysql google-cloud-platform google-bigquery google-cloud-sql