Oracle立即执行

时间:2018-01-30 09:31:33

标签: oracle dynamic select-into

有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个表,每个表都有类似的名称和列TableName_ID,其中包含序列,因此我创建了一个选择一定数量的数据的过程(P_Bucket)来自表格(P_Table)。 它还会选择选定的总行数P_COUNT并返回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

时收到错误OPEN P_DATA FOR

1 个答案:

答案 0 :(得分:2)

您必须从动态SQL中删除分号;另外,您必须在第二个动态查询中删除空格:' _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;