使用光标和循环-Snowflake SQL(经典的Web界面)将值插入新创建的表中的新创建的表中

发布于 2025-02-11 05:48:24 字数 997 浏览 2 评论 0 原文

我试图使用已经创建的表,光标和用于循环的表中的数据将值插入经典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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万人眼中万个我 2025-02-18 05:48:24

有许多类型的动态SQL无法处理光标名称,因此如果将其推入单个名称temp值,则会给出此错误:

  for record in c1 do
        let temp_price number := record.price;
        insert into invoice_final(study_number, price, price_type)
        values('1', temp_price, 'Dollars');
  end for;
  • 该SQL尚未运行,可能是错误的格式,但这是基本问题。

另外,这确实是插入物可以工作的,但我也认为这是简化问题的本质。

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:

  for record in c1 do
        let temp_price number := record.price;
        insert into invoice_final(study_number, price, price_type)
        values('1', temp_price, 'Dollars');
  end for;
  • this sql has not been run, and could be the wrong format, but it is the base issue.

Also this really looks like an INSERT would work, but I also assume this is the nature of simplify the question down.

宛菡 2025-02-18 05:48:24

有关使用变量的详细信息,请参见以下信息:
https> https:https:https://docs.sonowflake。 com/en/en/devpricer-guide/snowflake-sripting/variables.html#working-with-with-variables

下面的修订代码可根据需要函数:

-- Creation and inserting values into table invoice_original
        create
        or replace 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
        or replace temporary table invoice_final (
            study_number varchar,
            price number(12, 2),
            price_type varchar
        );
    execute immediate $
    declare
      
      new_price number(12,2);
      c1 cursor for select price from invoice_original;
    begin
      for record in c1 do
            new_price := record.price;
            insert into invoice_final(study_number, price, price_type) values('1',:new_price, 'Dollars');
      end for;
    end;
    $;

请注意,我将价格表定义更改为价格的目标表定义为数字(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:

-- Creation and inserting values into table invoice_original
        create
        or replace 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
        or replace temporary table invoice_final (
            study_number varchar,
            price number(12, 2),
            price_type varchar
        );
    execute immediate $
    declare
      
      new_price number(12,2);
      c1 cursor for select price from invoice_original;
    begin
      for record in c1 do
            new_price := record.price;
            insert into invoice_final(study_number, price, price_type) values('1',:new_price, 'Dollars');
      end for;
    end;
    $;

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文