I am creating a procedure in Bigquery
to which I want to pass a parameter, in this case the name of a table. But if nothing is passed to the procedure, it will assign a value by default.
This is what I currently have:
CREATE OR REPLACE PROCEDURE `MY_DATASET.MY_PROCEDURE`(tableName STRING)
BEGIN
DECLARE tableName STRING DEFAULT "MY_TABLE";
DECLARE queryString STRING;
SET queryString = SELECT * FROM MY_DATASET.tableName;
END;
This is the error it currently shows me:
Variable 'tableName' previously declared as an argument
If I don't pass any parameters to the procedure, the query is as follows:
SELECT `MY_DATASET.MY_PROCEDURE`();
queryString = SELECT * FROM MY_DATASET.MY_TABLE;
But if I pass a table to the procedure, I need it to create the following query:
SELECT `MY_DATASET.MY_PROCEDURE`('TABLE_TEST');
queryString = SELECT * FROM MY_DATASET.TABLE_TEST;
You may try and consider the below possible workaround.
Since passing a NULL
value on a declared parameter will result to expects 1 argument(s), 0 provided
error, you may just pass an empty string and then use an IF statement
to handle your desired default value if an empty string is passed.
CREATE OR REPLACE PROCEDURE `MY_DATASET.MY_PROCEDURE`(tableName STRING)
BEGIN
DECLARE table_Name STRING;
DECLARE queryString STRING;
SET table_Name = IF (tableName = '', 'MY_TABLE', tableName);
SET queryString = " SELECT * FROM MY_DATASET."||table_Name||"";
SELECT queryString;
END
Output using empty string parameter:
CALL SELECT `MY_DATASET.MY_PROCEDURE`('');
Output using a passed parameter:
CALL `MY_DATASET.MY_PROCEDURE`('TABLE_TEST');
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.