您可以使用光标来执行此操作。这样您就不必担心 TOO_MANY_ROWS 或 NO_DATA_FOUND 异常。
而且,您每次在查询中添加一列时,它都会自动添加到您的相同类型的变量中
光标有两个选项:仅使用返回的第一行或使用所有行。
选项1
DECLARE
CURSOR C_DATA IS
SELECT
table.col1, -- Column 2 intentionally left out
table.col3,
table.col4,
table2.column --Column from joined table
FROM table
JOIN table2
On table.col6 = table2.col1;
myRow C_DATA%rowtype
BEGIN
OPEN C_DATA;
FETCH c_data INTO myRow;
CLOSE C_DATA;
-- USE ANYWHERE INSIDE THIS ESCOPE YOUR COLUMNS as myRow.col4.
END;
选项#2
DECLARE
CURSOR C_DATA IS
SELECT
table.col1, -- Column 2 intentionally left out
table.col3,
table.col4,
table2.column --Column from joined table
FROM table
JOIN table2
On table.col6 = table2.col1;
BEGIN
FOR myRow IN C_DATA LOOP
-- USE INSIDE HERE YOUR COLUMNS as myRow.col4.
END LOOP;
END;