BigQuery Stored Procedure - Use variable in UPDATE statement for table name

南笙酒味 提交于 2021-02-17 06:25:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!