BigQuery - How to use 'call <stored.procedure>' or output of a procedure in a Select, Insert, With statement

Viewed 235

Want to do something like this:

Create Function dataset.sp_1(v_table STRING, v_ymd DATE) as ( BEGIN

DECLARE v_select, v_columns, v_dl_table, v_sbx_table, v_filter STRING;
Case v_table
    WHEN "tab1"      THEN 
        SET v_dl_table  = 'dl_dataset.tab1';
        SET v_sbx_table = 'sbx_dataset.tab1';
        SET v_columns   = 'Timestamp, col1, col2, col3';
        SET v_filter    = 'AND ColId IS NOT NULL';
End CASE

);

Set v_select = 'SELECT ' || v_columns || ' FROM ' || v_sbx_table|| ' WHERE DATE(Timestamp) = "' || v_ymd || '" ' || v_filter;
EXECUTE IMMEDIATE v_select;

END;

call dataset.sp_1("tab1","2022-01-05"); ---- Works fine.

=-=--= How to make this work=-=-=

Select * from (call dataset.sp_1("tab1","2022-01-05"));

OR

WITH C as (call dataset.sp_1("tab1","2022-01-05")) select * from C;

=-=-=-=-=

0 Answers
Related