首页 > 解决方案 > 在插入语句 plsql 中使用 if else

问题描述

我有将数据从 table1 插入 table2 的 plsql 代码

我想把 if 语句插入特定列中的某个值,这是我的代码

for rec_table1 in (
select * from table1 
where salary > 100)
loop
insert into table2 (
col1 , col2 , col3 col4 ,col5)
values 
(rec_table1.col1 , 
 rec_table1.col2,
 rec_table1.col3 , 
 rec_table1.col4 ,
if (rec_table1.col2 = 1)
  then
rec_table1.col2 
else
rec_table1.col5
end if  

但它在 if 语句中给了我语法错误

标签: sqloracleplsql

解决方案


一个普通的插入到外壳中就可以了。没有必要去循环。

  INSERT INTO table2 (
     col1,col2,col3,col4,col5) 
        select col1 , col2,col3 col4,CASE col2
     WHEN 1 THEN col2
          ELSE col5
     END AS col5
     FROM table1 where salary > 100;

推荐阅读