簡體   English   中英

sql / plsql-將源表中的行插入目標中,目標中的一列取決於源表中的另一列

[英]sql/plsql - insert rows from source table to target with one column in target depending on another column in Source table

我有一個要求,我必須從一個表(例如源)到另一個表(例如目標)插入行(幾百行)。 但是目標中有一列(例如錯誤列),其每一行的值取決於源中的一列(例如電子郵件)。 例如,如果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 –clause

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

可以寫成

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

見喜

2. Case when子句與If else子句相似/相同

 insert into select 

當源和目標中的數據類型匹配時,該語句從源復制數據並將其插入目標。

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

可以寫成

   Insert into targert select col,col2 from source 

當目標僅包含兩列時

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM