【发布时间】:2013-12-10 08:42:28
【问题描述】:
我有两张桌子TAB1 和TAB2。我想在特定列的TAB2 中插入数据。我想在WHERE 条件下插入必须从TAB1 获取的数据。如果存在 fethced 数据,则在 TAB2 中插入数据。如果不是,请在TAB2 中插入空白
这里我只讨论单列。我也在插入一些其他数据。它可以作为单个 SQL 查询发生吗?我正在使用 Oracle。
这是我的演示代码。我需要这样的东西:
INSERT INTO TAB1('value1','value2',?)
if exists(select king from TAB2 where queen='yash' )
?=select king from TAB2 where queen='yash'
if not exists
?=''
TAB1 和 TAB2 具有不同类型的列。
TAB1 TAB2
-------- -----------------
col1 col2 col3 col4 col1 king Queen
----------------- ----------------
1 jan King Robin 1 Alex yash
2 Feb King2 Hood 2 Jhon Shakthi
现在我想用 where 条件从 TAB2 向 TAB1 中插入数据 像这样的
insert into TAB1(col1,col2,col3,col4)
values(5,'MArch',King7, select king From TAB2 where Queen='yash')
如果 select king From TAB2 其中Queen='yash' 返回'Alex',那么如果不是必须插入空白值,则必须插入。
Here Queen column is Unique column .. So you can expect single row data only.
TAB1 and TAB2 having different column names and different column Types
【问题讨论】:
-
“如果不在 TAB2 中插入空白”这句话是什么意思
-
好的,你能给我们一些来自
TAB2的示例内容,以及你想在每种情况下插入TAB1的内容吗?当queen = 'yash'有多行时应该发生什么?如果存在 no 行,但该表有多个其他条目,会发生什么情况?如果有多个条目,当其中一些具有匹配值但不是全部时会发生什么? -
我已经编辑了我的问题..