Oracle 存储过程中执行动态合成SQL语句输出多条记录

CREATE OR REPLACE 
PROCEDURE ~~~~~~~~--(过程名称)

(
  v_id IN NUMBER ,
  v_cid IN NUMBER ,
	cv_1 Out Sys_RefCursor  --声明游标
)
AS
   v_SQL VARCHAR2(4000);
   v_FName VARCHAR2(50);
   v_FField VARCHAR2(50);
   CURSOR Cur
     IS SELECT FName ,
   FField 
     FROM ~~~~~~
    WHERE FID = v_cid
     ORDER BY FIndex;

BEGIN

   v_SQL := '' ;
   OPEN Cur;
   FETCH Cur INTO v_FName,v_FField;
   
   WHILE Cur%FOUND 
   LOOP 
      
      BEGIN
         v_SQL := v_SQL || v_FField || ',' || v_FField || ',' || v_FField || ',' ;
         FETCH Cur INTO v_FName,v_FField;
      END;
    
   END LOOP;
   CLOSE Cur;
   v_SQL := SUBSTR(v_SQL, 0, LENGTH(v_SQL) - 1) ;
   v_SQL := 'Select ' || v_SQL || ' From ~~~ where FID>0 and (FCID=:v_cid or :v_cid=0) and (FID=:v_id or :v_id=0)' ;--合成的语句中不能带;
   Open cv_1 For v_SQL Using v_cid,v_cid,v_id,v_id;
   
END;

 

你可能感兴趣的:(oracle)