问题
I am trying to use a while loop in a stored procedure to update a list of tables. When trying to execute the below code, I am getting the error: Table name "table_name" missing dataset while no default dataset is set in the request
. It seems the stored procedure is not correctly reading the variable table_name
when it is after UPDATE
. Is this intentionally not supported?
DECLARE table_names ARRAY<STRING>;
DECLARE table_name STRING;
DECLARE INDEX INT64 DEFAULT 0;
SET table_names = [
"`dev.table`",
"`dev.table2`"
];
BEGIN
WHILE INDEX < 2 DO
SET table_name = table_names[OFFSET(INDEX)];
SELECT table_name;
UPDATE table_name
SET name = "new_name"
WHERE name = "old_name";
SET INDEX = INDEX + 1;
END WHILE;
END
回答1:
in below fragment of script
UPDATE table_name
SET name = "new_name"
WHERE name = "old_name";
you should use EXECUTE IMMEDIATE - something like below
EXECUTE IMMEDIATE '''
UPDATE ''' || table_name || '''
SET name = "new_name"
WHERE name = "old_name"''';
来源:https://stackoverflow.com/questions/63342344/bigquery-stored-procedure-use-variable-in-update-statement-for-table-name