可以将绑定变量连接到动态 SQL WHERE 子句以添加 AND 语句吗?

Can a bind variable be concatenated to a dynamic SQL WHERE clause in order to add an AND statement?

我正在尝试使用绑定变量将 AND 语句添加到动态 WHERE 子句,但我收到以下 Oracle 错误:

ORA-01830: 日期格式图片在转换整个输入字符串绑定变量之前结束

这是有问题的代码:

FUNCTION WhereClause RETURN VARCHAR2
IS
BEGIN
    where_sql := 'WHERE TRUNC( ' || parm_rec.SRC_DATE_COLUMN || ' ) < ADD_MONTHS( ' ||
                                  'ADD_MONTHS ( TRUNC (  NVL ( :SYS_OFFSET, SYSDATE ) - ( :DAY_OFFSET )), ' ||
                                  '( :MON_OFFSET * :kNEGATIVE ) ), ' ||
                                  '( :YR_OFFSET * ( :kANNUM * :kNEGATIVE ) ) ) ';

    RETURN where_sql;
END WhereClause;



PROCEDURE ArchiveSrcDateFilter
IS     
BEGIN
DBMS_OUTPUT.PUT_LINE('ArchiveDynamic - ENTER');
IF parm_rec.SRC_DATE_COLUMN IS NULL THEN parm_rec.SRC_DATE_COLUMN := 'NULL';
END IF;
    FOR i in tbl_cur
    LOOP
    where_sql := WhereClause; -- defines the WHERE clause (where_sql) via function, Spec will not return variable to body?
    /*** DYNAMIC SQL DECLARATIONS ***/
        arc_sql := 'DECLARE ' ||
                    /*** DYNAMIC %ROWTYPE SELECT ***/
                        'CURSOR arc_cur IS ' ||
                        'SELECT * '||
                        'FROM '  || i.ARC_TABLE_NAME || '; '||               --obtain ARCHIVE ARC_SCHEMA_NAME.ARC_TABLE_NAME
                        'TYPE arc_cur_type IS TABLE OF arc_cur%ROWTYPE; ' || -- dynamically set archive record cursor %ROWTYPE for BULK COLECT as table collection 
                        'arc_rec    arc_cur_type; ' ||                       -- define archive record as TABLE OF cursor.%ROWTYPE          

                    /*** ARCHIVE PARAMETERS CURSOR - SRC_CREATE_DATE IS NOT NULL***/
                        'CURSOR parm_cur IS '||
                        'SELECT :seq_val AS ARCHIVE_ID, '||
                        'A.*, ' ||
                        'SYSDATE AS ARCHIVE_DATE ' ||
                        'FROM ' || srcSchemaTable || ' A ' ||                -- archive SRC_SCHEMA_NAME.SRC_TABLE_NAME (source table not archive table)
                        where_sql || ' || :ADD_FILTER ' || ' ; ' ||

               /*** DYNAMIC SQL STATEMENT BODY ***/                   
                   'BEGIN '||
                        'IF parm_cur%ISOPEN THEN CLOSE parm_cur; ' ||
                        'END IF; ' ||
                        'OPEN parm_cur; ' ||
                        'LOOP ' ||
                            'FETCH parm_cur ' ||
                            'BULK COLLECT INTO arc_rec LIMIT 500; ' ||
                            'EXIT WHEN arc_rec.COUNT = 0; ' ||
                            'FORALL i IN 1..arc_rec.COUNT ' ||
                                'INSERT INTO ' || arcTable ||
                                ' VALUES arc_rec( i );' ||
                                'DBMS_OUTPUT.PUT_LINE( ''ARC_REC_COUNT: '' || arc_rec.COUNT ); ' ||
                        'END LOOP; ' ||
                        'CLOSE parm_cur; ' ||                                   
                        'DBMS_OUTPUT.PUT_LINE(''SUCCESS...''); '||
                    'END; ';
    DBMS_OUTPUT.PUT_LINE('ArchiveDynamic - INSIDE LOOP: ' || arc_sql );

        EXECUTE IMMEDIATE arc_sql
        USING seq_val, parm_rec.SYS_OFFSET, parm_rec.DAY_OFFSET, parm_rec.MON_OFFSET, kNEGATIVE, parm_rec.YR_OFFSET, kANNUM, parm_rec.ADD_FILTER;

    END LOOP;

END ArchiveSrcDateFilter;

这是 :ADD_FILTER 绑定变量所在的 ArchiveSrcFilter 过程中的特定代码段(注意:我已尝试对绑定变量进行不同的串联迭代,但没有成功,这只是我在此处发布问题之前的最后一次尝试) :

'CURSOR parm_cur IS '||
'SELECT :seq_val AS ARCHIVE_ID, '||
'A.*, ' ||
'SYSDATE AS ARCHIVE_DATE ' ||
'FROM ' || srcSchemaTable || ' A ' ||
where_sql || ' || :ADD_FILTER ' || ' ; ' ||

并且使用最后一个参数作为绑定的 EXECUTE IMMEDIATE USING:

EXECUTE IMMEDIATE arc_sql
USING seq_val, parm_rec.SYS_OFFSET, parm_rec.DAY_OFFSET, parm_rec.MON_OFFSET, kNEGATIVE, parm_rec.YR_OFFSET, kANNUM, parm_rec.ADD_FILTER;

parm_rec.ADD_FILTER = AND STATUS = 1062

是否可以通过将绑定连接到 where 来完成我正在尝试的操作?

我不明白我收到的奇怪错误消息,因为如果我连接 parm_rec.ADD_FILTER 对象变量或硬编码 AND STATUS = 1062.

,代码将无一例外地执行

我可以连接 parm_rec.ADD_FILTER 来代替绑定变量,并且代码无一例外地执行,但是我尝试让绑定变量起作用的尝试一直没有成功。

非常感谢任何建议and/or 见解。

谢谢!

没有。列名(即 STATUS)和运算符(即 AND=)永远无法从绑定变量中解析。