Debugging BigQuery Stored procedure

梦想与她 提交于 2021-01-04 05:54:09

问题


Is there any way I can use print statements within BigQuery stored procedure? I have a stored procedure like below, I like to see how SQL statement is generated to debug the issue or any other better way to debug what stored procedure is producing etc.

CREATE OR REPLACE PROCEDURE `myproject.TEST.check_duplicated_prc`(project_name STRING, data_set_name STRING, table_name STRING, date_id DATE)
BEGIN
  DECLARE sql STRING;
  set sql ='Select date,col1,col2,col3,count(1) from `'||project_name||'.'||data_set_name||'.'||table_name|| '` where date='||date_id ||' GROUP BY date,col1,col2,col3 HAVING COUNT(*)>1';
    --EXECUTE IMMEDIATE (sql);
    print(sql)
END;

回答1:


There are number of approaches for debugging / troubleshooting stored proc

One of the simplest - to see how SQL statement is generated - slightly adjust your stored proc as in below example

CREATE OR REPLACE PROCEDURE `myproject.TEST.check_duplicated_prc`(project_name STRING, data_set_name STRING, table_name STRING, date_id DATE, OUT sql STRING)
BEGIN
  -- DECLARE sql STRING;
  set sql ='Select date,col1,col2,col3,count(1) from `'||project_name||'.'||data_set_name||'.'||table_name|| '` where date='||date_id ||' GROUP BY date,col1,col2,col3 HAVING COUNT(*)>1';
    --EXECUTE IMMEDIATE (sql);
END;   

Then, you can run below to see generated SQL

DECLARE sql STRING;
CALL `myproject.TEST.check_duplicated_prc`('project_name', 'data_set_name', 'table_name', '2020-11-24', sql);
SELECT sql;    

with output

As you can see here - you are missing apostrophes here where date=2020-11-24 so you can fix your stored proc



来源:https://stackoverflow.com/questions/64989997/debugging-bigquery-stored-procedure

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