gpt4 book ai didi

sql - Oracle SQL insert into with with 子句

转载 作者:行者123 更新时间:2023-12-03 11:34:29 26 4
gpt4 key购买 nike

我是 sql 的新手,所以这可能是一个愚蠢的问题,但是有没有可能将 With 子句与 Insert Into 一起使用?或者有什么常见的解决方法?我的意思是这样的:

With helper_table As (
Select * From dummy2
)
Insert Into dummy1 Values (Select t.a From helper_table t Where t.a = 'X' );

谢谢!

我的例子太假了,所以我添加了一些扩展代码(感谢到目前为止的答案)。
INSERT
INTO dummy values (a,b) //more values
WITH helper_table AS
(
SELECT *
FROM dummy2
)
WITH helper_table2 AS //from more tables
(
SELECT *
FROM dummy3
)
SELECT t.value as a, t2.value as b
FROM helper_table t
join helper_table t2 on t.value = t2.value //some join
WHERE t.value = 'X' and t2.value = 'X' //other stuff

最佳答案

您可以根据需要使用任意数量的“helper_tables”。

create table t(helper1 varchar2(50) , helper2 varchar2(50) , dataElement varchar2(50) );


insert into t(helper1, helper2, dataelement)
with
de as(select level lvl from dual connect by level <10)
,h1 as (select lvl, lvl/1.5 hp from de)
,h2 as (select lvl, lvl/2 hp2 from de)
select h1.hp , h2.hp2, de.lvl
from de
inner join
h1 on de.lvl = h1.lvl
inner join
h2 on de.lvl = h2.lvl
/

考虑到这一点,您可以通过将表正常连接到主表来完成所有连接

关于sql - Oracle SQL insert into with with 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5885154/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com