Oracle 立即执行

Oracle execute immediate

有 oracle 的东西,我不知道问题出在哪里,如果你能帮我解决这个问题

ORA-00911: invalid character
ORA-06512: at line 20

第 20 行是 EXECUTE IMMEDIATE SQL_STMT INTO P_COUNT USING L_RB_OD, L_RB_DO;

我有 10-15 个 table,它们每个都有相似的名称和包含序列的 TableName_ID 列,所以我制作了一个程序 select来自 table (P_Table) 的一定数量的数据 (P_Bucket)。 它还会将 select 总行数 select 编辑到 P_COUNT 和 return P_DATA sys_refcursor 中。过程编译成功但不会执行。

DECLARE
    P_PAR_01 VARCHAR2(5) := '2';
    P_BUCKET NUMBER := 200;
    P_TABLE VARCHAR2(15) := 'KB_FL_1';
    P_COUNT NUMBER;
    P_DATA SYS_REFCURSOR;
    L_RB_OD NUMBER;
    L_RB_DO NUMBER;
    L_RB NUMBER;
    SQL_STMT VARCHAR2(1000);
BEGIN
    L_RB := CAST(P_PAR_01 AS NUMBER);
    L_RB_DO := L_RB * P_BUCKET;
    L_RB_OD := L_RB_DO - (P_BUCKET - 1);

    SQL_STMT := 'SELECT COUNT(*)
                   FROM ' || P_TABLE || ' 
                   WHERE 1 = 1
                         AND ' || P_TABLE || '_ID BETWEEN :1 AND :2'; -- was :2;';

    EXECUTE IMMEDIATE SQL_STMT INTO P_COUNT USING L_RB_OD, L_RB_DO;

    OPEN P_DATA FOR 
    'SELECT * FROM ' || P_TABLE || '  
     WHERE 1 = 1
           AND ' || P_TABLE || ' _ID BETWEEN :1 AND :2
     ORDER BY ID_LICA' USING L_RB_OD, L_RB_DO;  -- was ORDER BY ID_LICA;'    
END;

编辑: 正如评论所说,动态 SQL 不使用分号,但即使删除分号后我也会在 [= 处收到错误 invalid character 19=]

您必须从动态中删除分号 SQL;另外,您必须在第二个动态查询中删除 space:' _ID' 应该是 '_ID'。 这应该有效:

DECLARE
    P_PAR_01 VARCHAR2(5) := '2';
    P_BUCKET NUMBER := 200;
    P_TABLE VARCHAR2(15) := 'KB_FL_1';
    P_COUNT NUMBER;
    P_DATA SYS_REFCURSOR;
    L_RB_OD NUMBER;
    L_RB_DO NUMBER;
    L_RB NUMBER;
    SQL_STMT VARCHAR2(1000);
BEGIN
    L_RB := CAST(P_PAR_01 AS NUMBER);
    L_RB_DO := L_RB * P_BUCKET;
    L_RB_OD := L_RB_DO - (P_BUCKET - 1);

    SQL_STMT := 'SELECT COUNT(*)
                   FROM ' || P_TABLE || ' 
                   WHERE 1 = 1
                         AND ' || P_TABLE || '_ID BETWEEN :1 AND :2';
    EXECUTE IMMEDIATE SQL_STMT INTO P_COUNT USING L_RB_OD, L_RB_DO;

    OPEN P_DATA FOR 
    'SELECT * FROM ' || P_TABLE || '  
     WHERE 1 = 1
           AND ' || P_TABLE || '_ID BETWEEN :1 AND :2
     ORDER BY ID_LICA' USING L_RB_OD, L_RB_DO;  


END;