sql - 在插入语句 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 语句中给了我语法错误
解决方案
一个普通的插入到外壳中就可以了。没有必要去循环。
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;
推荐阅读
- android - 无法创建布局资源文件
- reactjs - 调用 Search 函数后如何保留新组件?
- unit-testing - 如何测试回调函数?VUE/JEST
- onem2m - `child-resources` 的模式在哪里指定?
- java - 为 Minecraft Modding 设置工作场所时出错
- machine-learning - 您可以使用文本相似性搜索相关的数据库表/字段吗?
- django - Django:在下拉列表中排序项目(通过 field_order?)
- php - php如何从两个不同的关联数组中检索数据
- javascript - 在 React 中导入图像
- python - 为什么 BeautifulSoup 给我错误的文字?