英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论