我的要求有点不同。我有两个嵌套的 FOR 循环来获取数据,并根据某些条件将排序的数据插入到表中。
原始代码如下:
FOR Name_1 IN First_Cursor
LOOP
FOR Name_2 IN Second_Cursor (Name_1.Table_Field_Name)
LOOP
IF (Condition)
THEN
Prevalidation of data
INSERT INTO <Table_Name>
VALUES (..............................);
ELSE
IF (Condition)
THEN
Prevalidation of data
UPDATE <Table_Name>
WHERE .....................;
END IF;
END IF;
END LOOP;
END LOOP;
我必须将整个代码转换为 FORALL
我尝试了很多代码,但没有成功,我认为语法本身是错误的
IF (Condition)
THEN
Prevalidation of data
FORALL Name_2 IN Second_Cursor (Name_1.Table_Field_Name) of Name_1
INSERT INTO <Table_Name>
VALUES (..............................);
ELSE
IF (Condition)
THEN
Prevalidation of data
FORALL Name_2 IN Second_Cursor (Name_1.Table_Field_Name) of Name_1
UPDATE <Table_Name>
WHERE .....................;
END IF;
END IF;
请在SQL中使用"case"语句,如下所示:
语法:
CASE [ expression ]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n
ELSE result
END
.SQL:
select column1,column2,
( case when col3>0 then col4 else col5 end) as 'col_name' ,
( case when col6='value' then col7 else col8 end) as 'col_name1'
from
table_name
where col_name13>45