雪花游标引发错误并按预期执行。

huangapple go评论52阅读模式
英文:

Snowflake Cursor throwing Error and executing as expected

问题

我尝试编写一个查询来从模式中提取所有表名,将它们附加到查询中,然后将每一行插入临时表格,但我一直在Snowflake中遇到一个奇怪的错误。这是我编写的查询:

DROP TABLE IF EXISTS table_names;

CREATE TEMPORARY TABLE table_names (tname TEXT);

DECLARE
c1 CURSOR FOR SELECT table_name 
              FROM information_schema.tables 
              WHERE table_schema = current_schema() 
              AND table_type = 'BASE TABLE' 
              ORDER BY table_name;

BEGIN
    FOR record IN c1 DO
        INSERT INTO table_names 
           SELECT GET_DDL('TABLE', record.table_name , true);
    END FOR;
END;

当我执行它时,我得到这个错误:

错误:'STATEMENT_ERROR' 在第7行的第4个位置:SQL编译错误:错误发生在第1行的第48个位置(第6行)无效的标识符 'RECORD.TABLE_NAME'(第6行)

关于这个问题的任何帮助将不胜感激。

英文:

I'm trying to write a query to fetch all table names from a schema, append them to a query and then insert each row into a temporary table but I keep getting a weird error from Snowflake. This is the query I wrote:

DROP TABLE IF EXISTS table_names;

CREATE TEMPORARY TABLE table_names (tname TEXT);

DECLARE
c1 CURSOR FOR SELECT table_name 
              FROM information_schema.tables 
              WHERE table_schema = current_schema() 
              AND table_type = 'BASE TABLE' 
              ORDER BY table_name;

BEGIN
    FOR record IN c1 DO
        INSERT INTO table_names 
           SELECT GET_DDL('TABLE', record.table_name , true);
    END FOR;
END;

When I execute it I get this error:

Error:'STATEMENT_ERROR' on line 7 at position 4 : SQL compilation error: error line 1 at position 48 (line 6)
invalid identifier 'RECORD.TABLE_NAME' (line 6)

Any help with this would be much appreciated.

答案1

得分: 1

你需要将游标属性的值分配给一个普通变量,并与你的SQL一起使用:

DECLARE
c1 CURSOR FOR SELECT table_name 
              FROM information_schema.tables 
              WHERE table_schema = current_schema() 
              AND table_type = 'BASE TABLE' 
              ORDER BY table_name;
v_table_name VARCHAR;

BEGIN
    FOR record IN c1 DO
        v_table_name := record.table_name;
        INSERT INTO table_names 
           SELECT GET_DDL('TABLE', v_table_name, true);
    END FOR;
END;
英文:

You need to assign the value of the cursor attribute to a regular variable and use it with your SQL:

DECLARE
c1 CURSOR FOR SELECT table_name 
              FROM information_schema.tables 
              WHERE table_schema = current_schema() 
              AND table_type = 'BASE TABLE' 
              ORDER BY table_name;
v_table_name VARCHAR;

BEGIN
    FOR record IN c1 DO
        v_table_name := record.table_name;
        INSERT INTO table_names 
           SELECT GET_DDL('TABLE', :v_table_name , true);
    END FOR;
END;

huangapple
  • 本文由 发表于 2023年6月16日 03:32:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定