评估动态PLSQL语句

时间:2018-10-11 14:56:38

标签: oracle plsql oracle11g dynamic-sql

此过程的目的是将记录类型的列合并为一行文本。

示例:SALARY= 80000, POSITION=ENG, ID=8

如何动态评估l_stmt。

请注意,我不想通过串联方式进行传统操作。

打开其他建议。

谢谢

CREATE TABLE Employee
(
   ID         INTEGER,
   POSITION   VARCHAR2 (32),
   SALARY     NUMBER
);

INSERT INTO EMPLOYEE (ID, POSITION, SALARY)
     VALUES (1, 'ENG', '100000');

INSERT INTO EMPLOYEE (ID, POSITION, SALARY)
     VALUES (2, 'PROGRAMMER', '80000');

COMMIT;


CREATE OR REPLACE PROCEDURE column_to_text (rec employee%ROWTYPE)
AS
   TYPE tb IS TABLE OF VARCHAR2 (30)
      INDEX BY BINARY_INTEGER;

   l_colnames   tb;
   l_stmt       VARCHAR2 (2500);
BEGIN
   SELECT column_name
     BULK COLLECT INTO l_colnames
     FROM sys.all_tab_cols
    WHERE table_name = 'EMPLOYEE';

   FOR i IN 1 .. l_colnames.COUNT
   LOOP
      l_stmt :=
         l_stmt || l_colnames (i) || '=' || 'REC.' || l_colnames (i) || ', ';

   END LOOP;

   DBMS_OUTPUT.PUT_LINE ( l_stmt );

   --SALARY=REC.SALARY, POSITION=REC.POSITION, ID=REC.ID,
END;

0 个答案:

没有答案