使用情况何时将带有值()子句-Snowflake SQL(经典Web界面)的新表插入新表中
我正在尝试根据原始表中的现有条件将插入到()和值()中,以创建一个新表。我确实有工作代码,该代码将插入到()和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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用
插入所有
。请参阅在这里a>更多。代码如上下面
产生以下
You need to use
INSERT ALL
. Refer here for more.Change code as below
Executing above will produce following result -
允许作为分支结构:
更改:
a)将插入插入选择
b)每个语句必须以
;
免责声明结束:当无法将代码重写为基于设定的方法。
The CASE statement is allowed as branching construct:
Changes:
a) using INSERT INTO SELECT
b) each statement must end with
;
Disclaimer: Using cursor loop and loop in general should be used when there is no way of rewriting the code to set-based approach.
我将尽快编写一些示例代码。
但是形式
插入(从值中的选择)
允许存在所有n个值线。并插入了在其中的情况和结果子选择中的情况逻辑。
如果您在雪花脚本中,则是使用我的意思是在这些插件周围使用的话,使用
我的意思,用“完整的SQL变量作为我的示例不是循环的一部分”,您可以像循环一样。因此,有一个块:
因此,对于您循环,三个
$
将用:
替换,但这仅插入一行。如果是,但也有示例在下面使用的情况。
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:
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.