oracle 我在SQL developer ORA-01008上运行以下代码时遇到了这个错误:并非所有变量都绑定

b1uwtaje  于 2023-04-29  发布在  Oracle
关注(0)|答案(1)|浏览(155)
DECLARE
    v_start_date INT := 20220302;
    objtosearch varchar2(50) := '%data%';
BEGIN
    WHILE v_start_date <= 20230331 LOOP
        EXECUTE IMMEDIATE ( '
    insert into bazey.data_usage
        SELECT
            ft.id_date,
            vs.nr_orgn,
            SUM(qt_upld_trfc + qt_dwld_trfc) usage_mbs
        FROM
                 (
                SELECT
                    *
                FROM
                    fct_trf
                WHERE
                        ft.id_date = '
                            || v_start_date
                            || '
                    AND x_file_name =: objtosearch
            ) ft
            JOIN voice_data_sample vs ON substr(ft.nr_orgn, - 9, 9) = vs.nr_orgn
        GROUP BY
            ft.id_date,
            vs.nr_orgn
    ' );
        COMMIT;
        v_start_date := to_number(to_char(to_date(v_start_date, 'YYYYMMDD') + 1, 'YYYYMMDD'));

    END LOOP;
END;
oyt4ldly

oyt4ldly1#

AND x_file_name =: objtosearch

这里有一个:绑定变量标记,不过应该删除它和名称之间的空格。当你在EXECUTE IMMEDIATE内部绑定时,要真正绑定它,你必须使用USING子句指定变量。它不会仅仅因为名称相同而自动匹配。如果你对objtosearch这样做,你真的应该对start_date做同样的事情:

EXECUTE IMMEDIATE '
    insert into bazey.data_usage
        SELECT
            ft.id_date,
            vs.nr_orgn,
            SUM(qt_upld_trfc + qt_dwld_trfc) usage_mbs
        FROM
                 (
                SELECT
                    *
                FROM
                    fct_trf
                WHERE   ft.id_date = :start_date
                    AND x_file_name =:objtosearch
            ) ft
            JOIN voice_data_sample vs ON substr(ft.nr_orgn, - 9, 9) = vs.nr_orgn
        GROUP BY
            ft.id_date,
            vs.nr_orgn
    ' USING var_start_date,objtosearch;

但是,如果我可以建议的话,你真的没有理由在这里使用EXECUTE IMMEDIATE。只有当对象名(列名、表名)或大型结构项(连接子句、查询块等)被调用时,我们才使用动态SQL。)需要以编程方式设置,并且可以更改。如果所有的变化都是你正在寻找的数据值,不要使用动态SQL,使用普通SQL:

insert into bazey.data_usage
            SELECT
                ft.id_date,
                vs.nr_orgn,
                SUM(qt_upld_trfc + qt_dwld_trfc) usage_mbs
            FROM
                     (
                    SELECT
                        *
                    FROM
                        fct_trf
                    WHERE
                            ft.id_date = v_start_date
                        AND x_file_name = objtosearch
                ) ft
                JOIN voice_data_sample vs ON substr(ft.nr_orgn, - 9, 9) = vs.nr_orgn
            GROUP BY
                ft.id_date,
                vs.nr_orgn;

相关问题