简体   繁体   中英

Can i create table or insert data from stored procedure results use CALL in BigQuery?

I want use stored procedure results how data to create table or insert. But my example isn't working

DECLARE startDate, endDate STRING;
SET startDate = '20200801';
SET endDate = '20200802';

CALL `myproject.dataset.procedure1`(startDate, endDate);

CREATE OR REPLACE TABLE `myproject.dataset.table`
PARTITION BY date

Your SQL implies that procedure returns an implicit dataset which could be used in later CREATE TABLE, but this is not how BigQuery store procedure works.

One way is to move the CREATE TABLE to inside the procedure

DECLARE startDate, endDate STRING;
SET startDate = '20200801';
SET endDate = '20200802';

CREATE PROCEDURE `myproject.dataset.procedure1`(startDate STRING, endDate STRING)
BEGIN
  CREATE OR REPLACE TABLE `myproject.dataset.table`
  AS SELECT ... FROM ... WHERE date >= startDate AND date <= endDate -- just my guess
  PARTITION BY date 
END;
CALL `myproject.dataset.procedure1`(startDate, endDate);

If for some reason you don't want CREATE TABLE in procedure body, alternatively, you may use a temp table to bring the resultset out of the procedure, like

DECLARE startDate, endDate STRING;
SET startDate = '20200801';
SET endDate = '20200802';

CREATE PROCEDURE `myproject.dataset.procedure1`(startDate STRING, endDate STRING)
BEGIN
  CREATE TEMP TABLE myQueryResults
  AS SELECT ... FROM ... WHERE date >= startDate AND date <= endDate -- just my guess
  PARTITION BY date 
END;
CALL `myproject.dataset.procedure1`(startDate, endDate);

CREATE OR REPLACE TABLE `myproject.dataset.table`
AS SELECT * FROM myQueryResults
PARTITION BY date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM