在Oracle 11g中执行Immediate

时间:2017-02-06 16:08:24

标签: oracle execute-immediate

我正在尝试在oracle中创建一个脚本来搜索大量的表并插入到我正在创建的临时表中,但是当我尝试执行除数字之外的任何操作时,我会遇到一些错误。

声明变量

    m_polCount NUMBER:= 0;
    m_product NUMBER:= 0;
    m_version NUMBER:= 0;
    m_plan NUMBER:= 0;
    m_policy NUMBER:= 0;
    m_pol_comm_date DATE:= TO_DATE('01-01-3000','DD-MM-YYYY');
    m_pol_end_date DATE:= TO_DATE('01-01-3000','DD-MM-YYYY');
    m_pol_status NUMBER:= 0;
    m_next_pre_renew_date DATE:= TO_DATE('01-01-3000','DD-MM-YYYY');
    m_next_renew_date DATE:= TO_DATE('01-01-3000','DD-MM-YYYY');
    m_last_renew_date DATE:= TO_DATE('01-01-3000','DD-MM-YYYY');
    m_cover_no NUMBER:= 0;
    m_cover_name VARCHAR(240):= 'Test';
    m_cover_start_date DATE:= TO_DATE('01-01-3000','DD-MM-YYYY');
    m_cover_end_date DATE:= TO_DATE('01-01-3000','DD-MM-YYYY');
    m_sum_assured NUMBER:= 0;
    m_cover_layer NUMBER:= 0;
    m_cover_prem_layer NUMBER:= 0;
    m_premium NUMBER:= 0;
    m_loading_type NUMBER:= 0;
    m_loading_name VARCHAR(200):= 'Test';
    m_basic_prem_loading SMALLINT:= 0;
    m_loading_start_date DATE:= TO_DATE('01-01-3000','DD-MM-YYYY');
    m_loading_end_date DATE:= TO_DATE('01-01-3000','DD-MM-YYYY');
    m_loading_perc NUMBER:= 0;
    m_loading_rate NUMBER:= 0;
    m_loading_prem NUMBER:= 0;
    m_calc_desc VARCHAR(240):= 'Test';
    m_inflation_rate NUMBER:= 0;
    m_agent_no NUMBER:= 0;

表创建

EXECUTE IMMEDIATE '
        CREATE TABLE I233_ACTIVE_P4L_POLICY_DATA
        (
        PRODUCT NUMBER,
        VERSION NUMBER,
        PLAN_NUMBER NUMBER,
        POLICY_NUMBER NUMBER,
        POLICY_COMM_DATE DATE,
        POLICY_END_DATE DATE,
        POLICY_STATUS NUMBER,
        NEXT_PRE_RENEWAL_DATE DATE,
        NEXT_RENEWAL_DATE DATE,
        LAST_RENEWAL_DATE DATE, 
        COVER_NUMBER NUMBER,
        COVER_NAME VARCHAR(240),
        COVER_START_DATE DATE,
        COVER_END_DATE DATE,
        SUM_ASSURED NUMBER,
        COVER_LAYER NUMBER,
        COVER_PREMIUM_LAYER NUMBER,
        ANNUAL_PREMIUM NUMBER,
        LOADING_TYPE NUMBER,
        LOADING_NAME VARCHAR(200),
        BASIC_PREM_LOADING SMALLINT,
        LOADING_START_DATE DATE,
        LOADING_END_DATE DATE,
        LOADING_PERC NUMBER,
        LOADING_RATE NUMBER,
        LOADING_PREM NUMBER,
        PREMIUM_CALC_DESC VARCHAR(240),
        INFLATION_RATE NUMBER,
        AGENT_NUMBER NUMBER
        )';

插入声明

EXECUTE IMMEDIATE '
        INSERT INTO I233_ACTIVE_P4L_POLICY_DATA
        VALUES(
        '||m_product||',
        '||m_version||',
        '||m_plan||',
        '||policy_rec.policy_no||',
        '||m_pol_comm_date||', <----This field
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        '||m_pol_status||',
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        '||m_cover_no||',
        '''',
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''), 
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        '||m_sum_assured||',
        '||m_cover_layer||',
        '||m_cover_prem_layer||',
        '||m_premium||',
        '||m_loading_type||',
        ''TEST3'',
        '||m_basic_prem_loading||',
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        '||m_loading_perc||',
        '||m_loading_rate||',
        '||m_loading_prem||',
        ''TEST3'',
        '||m_inflation_rate||',
        '||m_agent_no||')';

错误

ORA-00917:缺少逗号

如果我尝试以相同的方式进行日期差异

插入声明

EXECUTE IMMEDIATE '
        INSERT INTO I233_ACTIVE_P4L_POLICY_DATA
        VALUES(
        '||m_product||',
        '||m_version||',
        '||m_plan||',
        '||policy_rec.policy_no||',
        m_pol_comm_date, <----This field
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        '||m_pol_status||',
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        '||m_cover_no||',
        '''',
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''), 
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        '||m_sum_assured||',
        '||m_cover_layer||',
        '||m_cover_prem_layer||',
        '||m_premium||',
        '||m_loading_type||',
        ''TEST3'',
        '||m_basic_prem_loading||',
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
        '||m_loading_perc||',
        '||m_loading_rate||',
        '||m_loading_prem||',
        ''TEST3'',
        '||m_inflation_rate||',
        '||m_agent_no||')';

给我以下错误

ORA-00984:此处不允许列

尝试使用相同的方法插入任何VARCHAR字段时,我也会遇到同样的问题。

有没有办法可以使用执行立即数将日期/ varchar字段插入到上面创建的临时表中?

以上所有陈述都包含在BEGIN,END块

由于

1 个答案:

答案 0 :(得分:1)

假设您无法避免动态sql,请使用绑定变量。这样,您就不必担心数据类型转换等等。

类似的东西:

execute immediate '
    INSERT INTO I233_ACTIVE_P4L_POLICY_DATA
    VALUES(:m_product,
           :m_version,
           :m_plan,
           :policy_rec.policy_no,
           :m_pol_comm_date, <----This field
           TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
           :m_pol_status,
           TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
           TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
           TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
           :m_cover_no,
           '''',
           TO_DATE(''01-01-3000'',''DD-MM-YYYY''), 
           TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
           :m_sum_assured,
           :m_cover_layer,
           :m_cover_prem_layer,
           :m_premium,
           :m_loading_type,
           ''TEST3'',
           :m_basic_prem_loading,
           TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
           TO_DATE(''01-01-3000'',''DD-MM-YYYY''),
           :m_loading_perc,
           :m_loading_rate,
           :m_loading_prem,
           ''TEST3'',
           :m_inflation_rate,
           :m_agent_no)'
  USING :m_product,
        :m_version,
        :m_plan,
        :policy_rec.policy_no,
        :m_pol_comm_date,
        :m_pol_status,
        :m_cover_no,
        :m_sum_assured,
        :m_cover_layer,
        :m_cover_prem_layer,
        :m_premium,
        :m_loading_type,
        :m_basic_prem_loading,
        :m_loading_perc,
        :m_loading_rate,
        :m_loading_prem,
        :m_inflation_rate,
        :m_agent_no;