使用情况何时将带有值()子句-Snowflake SQL(经典Web界面)的新表插入新表中

发布于 2025-02-11 20:33:56 字数 1843 浏览 5 评论 0原文

我正在尝试根据原始表中的现有条件将插入到()和值()中,以创建一个新表。我确实有工作代码,该代码将插入到()和select and of子句中,但我试图查看是否有可能在值之外的语句中进行情况,以便将某些值插入新的情况基于条件的表。

-- Creation and inserting values into table invoice_original
create temporary table invoice_original (id integer, price number(12,2),
                                         purpose varchar);
insert into invoice_original (id, price, purpose) values
  (1, 11.11, 'Business'),
  (2, 22.22, 'Personal'),
  (3, 33.33, 'Business'),
  (4, 44.44, 'Personal'),
  (5, 55.55, 'Business');
  
  
--  Creates final empty table invoice_final
create temporary table invoice_final (
  study_number varchar,
  price number(12, 2),
  price_type varchar
);

代码:

execute immediate $$
declare
  new_price number(12,2);
  new_purpose varchar;
  c1 cursor for select price, purpose from invoice_original;
begin
  for record in c1 do
        new_price := record.price;
        new_purpose := record.purpose;
        
-- This code runs!                
       insert into invoice_final(study_number, price, price_type)
       select 1, :new_price, 'Dollars'
       where :new_purpose ilike '%Business%';

       insert into invoice_final(study_number, price, price_type)
       select 2, :new_price, 'Dollars'
       where :new_purpose not like '%Business%';



-- Does not run but this is what I'm trying to do instead  
        CASE  
        WHEN :new_purpose ilike '%Business%' then 
        insert into invoice_final(study_number, price, price_type) 
        values('1', :new_price, 'Dollars')
        ELSE 
        insert into invoice_final(study_number, price, price_type) 
        values('2', :new_price, 'Dollars') END
        
  end for;
end;
$$;

这只是我要整体尝试做的事情的简化示例,但是真的想知道在这种情况下,插入inseatt in in in inseart in in ins ins ins ins in of()values()是否可能是可能的。

I'm trying to use insert into() and values() based on an existing condition in my original table to create a new table. I do have working code that uses insert into() along with a select and where clause but I'm trying to see if it's possible to do a CASE WHEN statement outside of the values so that certain values are inserted into the new table based on a conditional.

-- Creation and inserting values into table invoice_original
create temporary table invoice_original (id integer, price number(12,2),
                                         purpose varchar);
insert into invoice_original (id, price, purpose) values
  (1, 11.11, 'Business'),
  (2, 22.22, 'Personal'),
  (3, 33.33, 'Business'),
  (4, 44.44, 'Personal'),
  (5, 55.55, 'Business');
  
  
--  Creates final empty table invoice_final
create temporary table invoice_final (
  study_number varchar,
  price number(12, 2),
  price_type varchar
);

Code:

execute immediate $
declare
  new_price number(12,2);
  new_purpose varchar;
  c1 cursor for select price, purpose from invoice_original;
begin
  for record in c1 do
        new_price := record.price;
        new_purpose := record.purpose;
        
-- This code runs!                
       insert into invoice_final(study_number, price, price_type)
       select 1, :new_price, 'Dollars'
       where :new_purpose ilike '%Business%';

       insert into invoice_final(study_number, price, price_type)
       select 2, :new_price, 'Dollars'
       where :new_purpose not like '%Business%';



-- Does not run but this is what I'm trying to do instead  
        CASE  
        WHEN :new_purpose ilike '%Business%' then 
        insert into invoice_final(study_number, price, price_type) 
        values('1', :new_price, 'Dollars')
        ELSE 
        insert into invoice_final(study_number, price, price_type) 
        values('2', :new_price, 'Dollars') END
        
  end for;
end;
$;

This is just a simplified example of what I'm trying to do as whole, but just really wondering if a case when insert into() values() is possible in this scenario.

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

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

发布评论

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

评论(3

記憶穿過時間隧道 2025-02-18 20:33:57

您需要使用插入所有。请参阅在这里a>更多。

代码如上下面

execute immediate $
declare
  new_price number(12,2);
  new_purpose varchar;
  c1 cursor for select price, purpose from invoice_original;
begin
  for record in c1 do
        new_price := record.price;
        new_purpose := record.purpose;

INSERT ALL
when npurpose ilike '%Business%' then
 into invoice_final(study_number, price, price_type)
 values ('1',nprice,'Dollars')
else
 into invoice_final(study_number, price, price_type)
 values('2',nprice,'Dollars')
select :new_price as nprice, :new_purpose as npurpose;

  end for;
end;
$;

产生以下

select * from INVOICE_FINAL;
-11结果
2美元2
执行22美元
133美元
更改44美元
156美元

You need to use INSERT ALL. Refer here for more.

Change code as below

execute immediate $
declare
  new_price number(12,2);
  new_purpose varchar;
  c1 cursor for select price, purpose from invoice_original;
begin
  for record in c1 do
        new_price := record.price;
        new_purpose := record.purpose;

INSERT ALL
when npurpose ilike '%Business%' then
 into invoice_final(study_number, price, price_type)
 values ('1',nprice,'Dollars')
else
 into invoice_final(study_number, price, price_type)
 values('2',nprice,'Dollars')
select :new_price as nprice, :new_purpose as npurpose;

  end for;
end;
$;

Executing above will produce following result -

select * from INVOICE_FINAL;
STUDY_NUMBERPRICEPRICE_TYPE
111Dollars
222Dollars
133Dollars
244Dollars
156Dollars
疯了 2025-02-18 20:33:57

允许作为分支结构:

更改:

a)将插入插入选择

b)每个语句必须以;

execute immediate $
declare
  new_price number(12,2);
  new_purpose varchar;
  c1 cursor for select price, purpose from invoice_original;
begin
  for record in c1 do
        new_price := record.price;
        new_purpose := record.purpose;
           
        CASE  
        WHEN :new_purpose ILIKE'%Business%' THEN
          INSERT INTO invoice_final(study_number, price, price_type) 
          SELECT '1', :new_price, 'Dollars';
        ELSE 
          INSERT INTO invoice_final(study_number, price, price_type) 
          SELECT '2', :new_price, 'Dollars';
        END CASE;
        
  end for;
end;
$;

免责声明结束:当无法将代码重写为基于设定的方法。

INSERT INTO invoce_final(study_number, price, price_type)
SELECT CASE WHEN purpose ILIKE'%Business%' THEN 1 ELSE 2 END,
       price,
       'Dollars'
FROM invoice_orginal;

The CASE statement is allowed as branching construct:

Changes:

a) using INSERT INTO SELECT

b) each statement must end with ;

execute immediate $
declare
  new_price number(12,2);
  new_purpose varchar;
  c1 cursor for select price, purpose from invoice_original;
begin
  for record in c1 do
        new_price := record.price;
        new_purpose := record.purpose;
           
        CASE  
        WHEN :new_purpose ILIKE'%Business%' THEN
          INSERT INTO invoice_final(study_number, price, price_type) 
          SELECT '1', :new_price, 'Dollars';
        ELSE 
          INSERT INTO invoice_final(study_number, price, price_type) 
          SELECT '2', :new_price, 'Dollars';
        END CASE;
        
  end for;
end;
$;

Disclaimer: Using cursor loop and loop in general should be used when there is no way of rewriting the code to set-based approach.

INSERT INTO invoce_final(study_number, price, price_type)
SELECT CASE WHEN purpose ILIKE'%Business%' THEN 1 ELSE 2 END,
       price,
       'Dollars'
FROM invoice_orginal;
甜宝宝 2025-02-18 20:33:57

我将尽快编写一些示例代码。

但是形式

插入(从值中的选择)

允许存在所有n个值线。并插入了在其中的情况和结果子选择中的情况逻辑。

如果您在雪花脚本中,则是使用我的意思是在这些插件周围使用的话,使用

我的意思,用“完整的SQL变量作为我的示例不是循环的一部分”,您可以像循环一样。因此,有一个块:

create temporary table invoice_final (
  study_number varchar,
  price number(12, 2),
  price_type varchar
);
 
set new_purpose = 'Business';
set new_price = 10.0::number(12, 2);
 
insert into invoice_final(study_number, price, price_type)
SELECT column1, column2, column3
FROM VALUES 
        ('1', $new_price, 'Dollars'),
        ('2', $new_price, 'Dollars')
WHERE CASE WHEN $new_purpose ilike '%Business%' then '1' ELSE '2' END = column1;       

因此,对于您循环,三个$将用替换,但这仅插入一行。

如果是,但也有示例在下面使用的情况。

I will write some example code soon.

But the form

INSERT INTO (SELECT FROM VALUES WHERE)

Allows all your N value lines to be present. And the CASE logic to be in the WHERE and the result SUB-SELECT is INSERTed.

The ofther option if you are in Snowflake Scripting is to use an IF around those INSERTS verse using a CASE

What I was meaning, done with "full SQL variables as my example is not part of a loop" is you can move the SQL around like so, and have one block:

create temporary table invoice_final (
  study_number varchar,
  price number(12, 2),
  price_type varchar
);
 
set new_purpose = 'Business';
set new_price = 10.0::number(12, 2);
 
insert into invoice_final(study_number, price, price_type)
SELECT column1, column2, column3
FROM VALUES 
        ('1', $new_price, 'Dollars'),
        ('2', $new_price, 'Dollars')
WHERE CASE WHEN $new_purpose ilike '%Business%' then '1' ELSE '2' END = column1;       

Thus for you loop, the three $ would be replaced with :, but this inserts just one row.

the IF was meaning are the Snowflake Scripting IF, but there is examples of using CASE just below that also.

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