How can we display all query results in PL/SQL?
In PL/SQL, you can use a cursor to fetch query results and loop through and display the results using loop statements. Here is an example code:
DECLARE
-- 声明游标
CURSOR c_result IS
SELECT * FROM your_table;
-- 声明变量来存储查询结果
-- 这里假设你的表中有两个字段:field1和field2
v_field1 your_table.field1%TYPE;
v_field2 your_table.field2%TYPE;
BEGIN
-- 打开游标
OPEN c_result;
-- 循环遍历结果集
LOOP
-- 获取下一个结果
FETCH c_result INTO v_field1, v_field2;
-- 检查是否还有结果
EXIT WHEN c_result%NOTFOUND;
-- 显示结果
DBMS_OUTPUT.PUT_LINE('Field1: ' || v_field1 || ', Field2: ' || v_field2);
END LOOP;
-- 关闭游标
CLOSE c_result;
END;
/
In the above code, we first declare a cursor called c_result to store the query results. Next, we declare variables v_field1 and v_field2 that are of the same data type as the fields in the table to store the values of each row’s result.
In the main part of the code, we open the cursor and use a LOOP statement to iterate through the result set. During each iteration of the loop, we use the FETCH statement to retrieve the next row of results and assign it to a variable. We then check if there are still results, and if not, we exit the loop.
Finally, we use the DBMS_OUTPUT.PUT_LINE function to display the values of each row. You can customize the output format according to your needs. At the end of the code, we close the cursor.
After running the above code, you should be able to see all query results printed out. Please note that you may need to enable the DBMS_OUTPUT feature in your programming environment in order to see the output results.