使用dbms_sql定义列

时间:2016-01-14 17:41:33

标签: plsql

我得到一个pls-00307错误,它说"字符串"匹配太多的对象表别名。这是什么意思? 我得到了第4节DEFINE COLUMN的一个亮点当我尝试在IF语句中定义一个列时,为什么会发生这种情况?谁能引导我通过这个?谢谢!

IF ((pStartDate IS NOT NULL) AND (pEndDate IS NOT NULL) AND
     (pStartDate < pEndDate)) THEN


IF ((pIdType IS NOT NULL) AND (pId IS NOT NULL)) THEN
  loSqLText1 := ' CBP.CBP_ID_TYPE, CBP.CBP_ID AS  ';
  loSqLText2 := ' CBP_ID_TYPE, CBP_ID ';
  loSqLText3 := ' CBP_ID_TYPE = :pidtype AND CBP_ID = :pid ';
END IF;

IF ((pName IS NOT NULL) AND (pSurname IS NOT NULL)) THEN
  loSqLText1 := ' CBP.CBP_NAME, CBP.Cbp_Surname ';
  loSqLText2 := ' CBP_NAME , CBP_SURNAME ';
  loSqLText3 := ' CBP_NAME = :pname AND CBP_SURNAME = :psurname ';
END IF;
END IF;

  --1 Query

  loQuery := 'SELECT ' || loSqLText1 ||
         ' , IN_OUT_DATES.IN_COUNT, IN_OUT_DATES.OUT_COUNT  
         FROM 
           (SELECT HBP_PERSON , COUNT(HBP_IN_DATE) IN_COUNT , COUNT(HBP_OUT_DATE) OUT_COUNT 
             FROM HIST_BANNED_PERSONS WHERE HBP_IN_DATE BETWEEN 
              to_date('':pstartdate'',''mm/dd/yyyy'') AND to_date( '':penddate'' , ''mm/dd/yyyy'' )
              GROUP BY HBP_PERSON ) IN_OUT_DATES
              RIGHT OUTER JOIN
              (SELECT CBP_ROWID, ' || loSqLText2 ||
         ' FROM CHECK_BANNED_PERSONS WHERE ' || loSqLText3 ||
         ' ) CBP
              ON IN_OUT_DATES.HBP_PERSON = CBP.CBP_ROWID ';
  --2 OPEN CURSOR



loCursor := dbms_sql.open_cursor();

  --3 PARSE
  dbms_sql.parse(loCursor, loQuery, dbms_sql.native);

  --4 DEFINE COLUMN 
  IF ((pIdType IS NOT NULL) AND (pId IS NOT NULL)) THEN
    dbms_sql.define_column(loCursor, 1, loIdType);
    dbms_sql.define_column(loCursor, 2, loId);
  END IF;

  IF ((pName IS NOT NULL) AND (pSurname IS NOT NULL)) THEN
    dbms_sql.define_column(loCursor, 1, loName);
    dbms_sql.define_column(loCursor, 2, loSurname);
  END IF;

  dbms_sql.define_column(loCursor, 3, loInDate);
  dbms_sql.define_column(loCursor, 4, loOutDate);

我得到的错误消息指出以下几行:

dbms_sql.define_column(loCursor, 1, loIdType);
dbms_sql.define_column(loCursor, 2, loId);

dbms_sql.define_column(loCursor, 1, loName);
dbms_sql.define_column(loCursor, 2, loSurname);

1 个答案:

答案 0 :(得分:1)

据我了解,您的变量loIdTypeloIdloNameloSurname都有VARCHAR2类型。在这种情况下,添加一个参数来定义变量长度:

dbms_sql.define_column(loCursor, 1, loIdType, 4000);
dbms_sql.define_column(loCursor, 2, loId, 4000);

dbms_sql.define_column(loCursor, 1, loName, 4000);
dbms_sql.define_column(loCursor, 2, loSurname, 4000);