如何输出使用本机动态 SQL 执行的 SELECT 语句的结果?

2021-12-24 00:00:00 oracle plsql

我有一个包含 SQL SELECT 语句的字符串.
我想知道如何在屏幕上输出该语句的执行结果,执行将使用本机动态 SQL (EXECUTE IMMEDIATE).

I have a string which contains SQL SELECT statement.
I wonder how can I output result of the execution of that statement on the screen, execution will be done using native dynamic SQL (EXECUTE IMMEDIATE).

示例:

DECLARE
v_stmt VARCHAR2 := 'SELECT * FROM employees';
BEGIN
EXECUTE IMMEDIATE v_stmt; -- ??? how to output result of that select on the screen.
END;

重要说明:表的结构可以是任意的.我必须编写一个接受表名作为参数的过程,所以我不能对表结构进行硬编码,也不想这样做.

Important remark: structure of table can be any. I have to write a procedure which accepts name of the table as parameter, so I can't hardcode a table structure and don't want to do it.

感谢您的回复.非常感谢任何想法/

Thanks for responses. Any ideas very appreciated/

推荐答案

如果您可以将立即执行更改为 dbms_sql 游标,那么以下解决方案应该可以帮助您,因为您可以从 dbms_sql 中获取列名光标:

If you can change that execute immediate into a dbms_sql cursor, then the following solution should be able to help you, as you can get the column names from a dbms_sql cursor:

https://forums.oracle.com/forums/thread.jspa?threadID=700648

相关文章