简体   繁体   中英

Storing results of a stored procedure in a Google Big Query table

Here is a link to the question I asked earlier. The accepted answer works perfectly.

But this stored procedure processes 2 statements and after running this procedure I have to click on View Results of the second statement to see the result in Google Big Query. Is there a way to save the results in some table automatically using the 'Query Settings' in Google Big Query and specifying the name of the table I want to store the results in?

You cannot set a destination table for a script (or for call a procedure), instead, you can convert your SELECT statement into CREATE TABLE ... AS SELECT , for example:

SELECT 1 x;

=>

CREATE TABLE myDataset.myTable
AS SELECT 1 x;

You can define a string parameter for your procedure. Then use this parameter in the dynamic query to write the results to the table.

CREATE OR REPLACE PROCEDURE `my_dataset.my_procedure`(destination_table STRING)
BEGIN

CREATE TEMP TABLE tmp AS SELECT 1 x;

EXECUTE IMMEDIATE (
    FORMAT("CREATE OR REPLACE TABLE `%s` AS SELECT * FROM tmp", destination_table));

END

Now you can provide a table name and call this procedure to write the results to the table.

CALL `my_dataset.my_procedure`("my_dataset.my_table");
SELECT * FROM `my_dataset.my_table` 

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