sql/pl将行从源表插入到目标,目标中的一列取决于源表中的另一列



我有一个要求,我必须将行(几百行)从一个表(比如源)插入到另一个表(比如目标)。但是目标中有一列(例如错误列),其每行的值取决于源中的一列(例如电子邮件)。 例如,如果 source.email 为 null,则 target.error = 'no email'(硬编码)否则 target.error = 其他(硬编码)。要在目标表中填充的所有其他列都存在于源表中。插入行的有效方法是什么?

1. insert into target select col1,col2, decode(email,null,'no 
email','others') col4, col5 from source;
2  insert into target select col1,col2, case when email is null then 'no 
email' else 'others' end col4 , col5 
from source.

1 解码函数的工作方式类似于 if else –子句

decode(email,null,'no  email','others') 

可以写成

if email is null then 'no  email' else 'others' end if

见更高

2.子句与"如果"条款相似/相同时的情况

insert into select 

语句从源复制数据,并在源和目标中的数据类型匹配时将其插入到目标中。

Insert into target(col1,col2) select col1, col2 from source 

可以写成

Insert into targert select col,col2 from source 

当目标仅包含两列时

相关内容

最新更新