我试图使用已经创建的表,光标和用于循环的表中的数据将值插入经典Snowflake SQL Web界面中的新表中。我的目标是将新信息和信息从原始表中插入新表中,但是当我尝试运行代码时,有一个错误,我指的是原始表的列。 (请参阅下面的代码)
-- Creation and inserting values into table invoice_original
create temporary table invoice_original (id integer, price number(12,2));
insert into invoice_original (id, price) values
(1, 11.11),
(2, 22.22);
-- Creates final empty table invoice_final
create temporary table invoice_final (
study_number varchar,
price varchar,
price_type varchar);
execute immediate $$
declare
c1 cursor for select price from invoice_original;
begin
for record in c1 do
insert into invoice_final(study_number, price, price_type)
values('1', record.price, 'Dollars');
end for;
end;
$$;
我的最终目标是将结果表Invoice_final带有3列 - study_number,Price和Price_type,价格值来自Invoice_original表。我目前遇到的错误是:
第6行上的第6行上的“ statement_error”类型除外:SQL编译错误:位置20处的错误行2在第20位无效标识符'Record.price'。
有谁知道为什么记录。价格没有从invoice_original表中捕获价格值?
I'm trying to insert values into a new table in the classic Snowflake SQL web interface using data from a table that was already created, a cursor, and a for loop. My goal is to insert new information and information from the original table into the new table, but when I try and run my code, there is an error where I am referring to the column of my original table. (See code below)
-- Creation and inserting values into table invoice_original
create temporary table invoice_original (id integer, price number(12,2));
insert into invoice_original (id, price) values
(1, 11.11),
(2, 22.22);
-- Creates final empty table invoice_final
create temporary table invoice_final (
study_number varchar,
price varchar,
price_type varchar);
execute immediate $
declare
c1 cursor for select price from invoice_original;
begin
for record in c1 do
insert into invoice_final(study_number, price, price_type)
values('1', record.price, 'Dollars');
end for;
end;
$;
My end goal is to have the resulting table invoice_final with 3 columns - study_number, price, and price_type where the price value comes from the invoice_original table. The error I'm currently getting is:
Uncaught exception of type 'STATEMENT_ERROR' on line 6 at position 8 : SQL compilation error: error line 2 at position 20 invalid identifier 'RECORD.PRICE'.
Does anyone know why the record.price is not capturing the price value from the invoice_original table?
发布评论
评论(2)
有许多类型的动态SQL无法处理光标名称,因此如果将其推入单个名称temp值,则会给出此错误:
另外,这确实是插入物可以工作的,但我也认为这是简化问题的本质。
there are a number of type of dynamic SQL that do not handle the cursor name, and thus give this error if you push it into a single name temp value it will work:
Also this really looks like an INSERT would work, but I also assume this is the nature of simplify the question down.
有关使用变量的详细信息,请参见以下信息:
https> https:https:https://docs.sonowflake。 com/en/en/devpricer-guide/snowflake-sripting/variables.html#working-with-with-variables
下面的修订代码可根据需要函数:
请注意,我将价格表定义更改为价格的目标表定义为数字(12,2)而不是VARCHAR,并将Record.price分配给一个局部变量,该变量已传递给插入语句为:new_price。
都说...出于绩效原因,我强烈建议您反对加载表的方法。您可以用插入物替换所有这些。
始终选择基于光标 /循环 /行的基于雪花处理的处理。
See the following for details on working with variables:
https://docs.snowflake.com/en/developer-guide/snowflake-scripting/variables.html#working-with-variables
The revised code below functions as desired:
Note that I changed the target table definition for price to NUMBER (12,2) instead of VARCHAR, and assigned the record.price to a local variable that was passed to the insert statement as :new_price.
That all said ... I would strongly recommend against this approach for loading tables for performance reasons. You can replace all of this with an INSERT .. AS ... SELECT.
Always opt for set based processing over cursor / loop / row based processing with Snowflake.
https://docs.snowflake.com/en/sql-reference/sql/insert.html