在用户定义的函数中执行immediate - 在内部查询

时间:2016-01-18 05:19:13

标签: stored-procedures plsql oracle11g

我正在使用存储过程从oracle数据库中获取记录。此过程返回基于提供的搜索输入构建动态SQL查询的分页记录。输出'状态'列来自用户定义的函数' GET_STATUS_FOR_ME'。一些派生列会根据每个记录的实际状态传递给此函数。

一切正常,但未提供状态作为输入搜索条件。当提供状态时,它会给我以下错误

    ORA-06535: statement string in EXECUTE IMMEDIATE is NULL or 0 length
ORA-06512: at "pkg_search", line 15
06535. 00000 -  "statement string in %s is NULL or 0 length"
*Cause:    The program attempted to use a dynamic statement string that
           was either NULL or 0 length.
*Action:   Check the program logic and ensure that the dynamic statement
           string is properly initialized.

我的用户定义功能如下:

FUNCTION GET_STATUS_FOR_ME(
    RECORDID     IN NUMBER,
    ASSIGNEDTO   IN NUMBER,
    LOCATIONID   IN NUMBER,
    TEMPUSERID   IN NUMBER,
    ACTUALSTATUS IN VARCHAR2)
  RETURN VARCHAR2
AS
  O_STATUS   VARCHAR2(200 BYTE);
  TEMP_QUERY VARCHAR2(200 BYTE);
  I_COUNT    NUMBER;
BEGIN
  IF LOCATIONID IS NOT NULL AND TEMPUSERID IS NULL THEN
    TEMP_QUERY  := 'SELECT COUNT(*) FROM MY_TABLE WHERE COUNTRYIF = ' || TO_CHAR(LOCATIONID);
    EXECUTE IMMEDIATE TEMP_QUERY INTO I_COUNT;
  END IF;

  . . . Other FUNCTION logic here. . .

    RETURN O_STATUS;
  END GET_STATUS_FOR_ME;

我调用此函数的过程如下所示。从内部查询调用此函数。

  PROCEDURE SEARCH_RESULT_PROC(
      I_LOGGEDINUSERID  IN NUMBER,
      I_CREATEDFROMDATE IN DATE,
      I_CREATEDTODATE   IN DATE,
      I_STATUS          IN VARCHAR2,
      I_COUNTRYID       IN NUMBER,
      I_LANGUAGEID      IN NUMBER,
      I_OFFSET          IN NUMBER,
      I_LIMIT           IN NUMBER,
      I_ORDRBY          IN VARCHAR2,
      I_SORTBY          IN VARCHAR2,
      O_COUNT OUT NUMBER,
      REF_CUS_RESULTS OUT SYS_REFCURSOR)
  AS
    PAG_END_ROW           NUMBER;
    STC_SQL_PART          VARCHAR2(9999 BYTE);
    DYNMC_SQL_CLAUSE_PART VARCHAR2(9999 BYTE);
  BEGIN
    PAG_END_ROW  := I_OFFSET + I_LIMIT - 1;
    STC_SQL_PART := 'select ID, REOCRDSTATUS, 
(CASE          
some logic here       
END) LOCATIONID, 
(CASE          
some logic here       
END) USERID from MY_TABLE MTABLE';

    . . Other Logic TO build dynamic WHERE clause depending ON inputs provided. . .

    IF I_DOCSTATUS          IS NOT NULL THEN
      DYNMC_SQL_CLAUSE_PART := DYNMC_SQL_CLAUSE_PART || ' AND UPPER(TEMPDATA.STATUS)  = UPPER(''' || I_STATUS || ''')';
    END IF;
    FINAL_QUERY := 'SELECT ODATA.EID,  
ODATA.TITLE,  
ODATA.TYPE,   
pkg_search.GET_STATUS_FOR_ME(ID,' || I_LOGGEDINUSERID || ',ODATA.LOCATIONID,ODATA.USERID,ODATA.REOCRDSTATUS) STATUS
FROM (SELECT ROWNUM RNUM , TEMP.* FROM ( ' || STC_SQL_PART || DYNMC_SQL_CLAUSE_PART || ' )TEMP WHERE ROWNUM <= ' || TO_CHAR(PAG_END_ROW) ||' ) ODATA WHERE ODATA.RNUM >= '|| TO_CHAR( I_OFFSET) ;
    OPEN REF_CUS_RESULTS FOR FINAL_QUERY;
  END SEARCH_RESULT_PROC;

0 个答案:

没有答案