使用截止日期ORACLE立即执行格式化

使用截止日期ORACLE立即执行格式化,oracle,plsql,to-date,execute-immediate,Oracle,Plsql,To Date,Execute Immediate,我被困在一个包中的格式化过程中。。。。。脚本可以正常工作,但是将其与包集成是很困难的 我不习惯oracle…我有脚本运行,但不在包中…不是所有的…Drop Table工作正常 CREATE OR REPLACE PACKAGE BODY Testing is PROCEDURE DropTable1 IS BEGIN execute immediate ('DROP TABLE mytable1');

我被困在一个包中的格式化过程中。。。。。脚本可以正常工作,但是将其与包集成是很困难的

我不习惯oracle…我有脚本运行,但不在包中…不是所有的…Drop Table工作正常

CREATE OR REPLACE PACKAGE BODY Testing is
    PROCEDURE DropTable1 IS         
        BEGIN
                execute immediate ('DROP TABLE mytable1');                           
        END;

    PROCEDURE PopulateTable1 IS
        BEGIN
            execute immediate ('CREATE TABLE mytable1
            AS (   
                select
                substr(t1.genarea,3,3) as M_Class,
                substr(t1.genarea,6,30) as M_Description,
                substr(t1.genarea,36,3) as M_Class,
                substr(t1.genarea,39,30) as M_Description,
                substr(t1.itemitem,1,3) as product_code,
                t3.CHANNEL_NUM as SALES_CHANNEL,
                to_date(''t2.time_id'',''dd-mon-yyyy'') as mis_date,
                sum(t2.ap_cw_cfi_irp+t2.ap_cw_issues_irp) as ap_gross,
                sum(t2.Ap_Cw_Cfi_Irp+t2.Ap_Revivals_Irp) as ap_net,
                sum(t2.sp_inc_irp+t2.sp_issues_irp) as sp_gross,
                sum(t2.sp_dec_irp+t2.sp_fs_irp) as sp_net
              from
                d_pr t1, act t2, age_map t3
              where
                t1.pfx=''IT'' and t1.coy=''1'' and t1.tabl=''T81'' and substr(t1.itemitem,1,3) = t2.product_id and t3.AGE_NUM = t2.age_id
              group by
                substr(t1.genarea,3,3),
                substr(t1.genarea,6,30),
                substr(t1.genarea,36,3),
                substr(t1.genarea,39,30),
                substr(t1.itemitem,1,3),
                t3.CHANNEL_NUM,
                to_date(''t2.time_id'',''dd-mon-yyyy'')
            )');
            COMMIT;
        END PopulateTable1;
END Testing;
/

谢谢

你不应该在
t2.time\u id
周围加引号。单引号用于表示字符串文字;这显然是一个列引用。

你不应该在
t2.time\u id
周围加引号。单引号用于表示字符串文字;这显然是一个专栏参考

BEGIN
execute immediate ('CREATE TABLE mytable1 AS 
(select to_date(/*remove single quotes here*/t2.time_id/*and here*/,''dd-mon-yyyy'') as mis_date 
from (select current_date time_id from dual) t2)');
end;
在select查询的最后一行中。因为它被视为字符串


在select查询的最后一行中。因为它被视为字符串,所以代码中有两件事看起来很奇怪:

  • 截止日期(“t2.时间id”,“dd-mon-yyyy”)
列周围不需要任何单引号,只需要字符串文字周围的单引号。将其更改为:

to_date(t2.time_id,''dd-mon-yyyy'')
如果您在多个位置使用单引号,那么我建议避免由于字符串中的单引号而导致错误。例如,请参见

  • COMMIT

对于DDL语句,不需要COMMIT,只需要对DML语句进行COMMIT。删除它。

代码中有两件事看起来很奇怪:

  • 截止日期(“t2.时间id”,“dd-mon-yyyy”)
列周围不需要任何单引号,只需要字符串文字周围的单引号。将其更改为:

to_date(t2.time_id,''dd-mon-yyyy'')
如果您在多个位置使用单引号,那么我建议避免由于字符串中的单引号而导致错误。例如,请参见

  • COMMIT

对于DDL语句,不需要COMMIT,只需要对DML语句进行COMMIT。删除它。

当您遇到这样的问题时,将字符串指定给变量,并使用DBMS_输出将其打印到屏幕上。当您遇到这样的问题时,将字符串分配给一个变量,并使用DBMS_输出将其打印到屏幕上。这将向您显示正在提交的确切SQL。