External_query with dynamic query - Error: Connection argument in EXTERNAL_QUERY must be a literal string or query parameter

心不动则不痛 提交于 2020-04-30 08:51:44

问题


I'm trying to call external_query with dynamically created query string in BigQuery Web UI.

DECLARE extquery STRING;
SET extquery = "SELECT * FROM mytable;";
SELECT * FROM EXTERNAL_QUERY("my-external-conn", extquery);

This script gives an error

Query error: Invalid table-valued function EXTERNAL_QUERY
Connection argument in EXTERNAL_QUERY must be a literal string or query parameter

Is it impossible to do in BigQuery? What is the right way to pass query as a parameter?


回答1:


As error states - "Connection argument in EXTERNAL_QUERY must be a literal string or query parameter"
So, "must be a literal string" version would be

SELECT * FROM EXTERNAL_QUERY("my-external-conn", "SELECT * FROM mytable;");    

It is more complicated with "must be a query parameter"
Note: query parameters and script variables are quite different animals - you can check details in respective docs - but to clarify my point : extquery in SET extquery = "SELECT * FROM mytable;"; is not a parameter but rather script variable

Meantime, BigQuery UI (neither Classic nor Console) do not support Parameterized Queries, but you still can make it using CLI, API or clients of your choice

Below example is for CLI

bq query \
--use_legacy_sql=false \
--parameter='extquery::SELECT * FROM mytable;' \
'SELECT * FROM EXTERNAL_QUERY("my-external-conn", @extquery)'  


来源:https://stackoverflow.com/questions/61293822/external-query-with-dynamic-query-error-connection-argument-in-external-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!