Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging BigQuery Stored procedure

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;
like image 557
Dinesh Avatar asked Sep 17 '25 14:09

Dinesh


1 Answers

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

enter image description here

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

like image 91
Mikhail Berlyant Avatar answered Sep 19 '25 03:09

Mikhail Berlyant