用于编写存储过程的plSQL语法,带有3个变量

时间:2015-11-12 22:51:47

标签: oracle plsql coldfusion

我有这么长的SQL语句块,我需要为接受3个变量创建一个存储过程。变量是invoice,NewDate,TransactionDate,数据类型分别是整数,日期和日期。我试图创建存储过程,但语法搞砸了我。我将使用Coldfusion和cfstoredproc调用存储过程,因此英镑符号。并不是说我需要ColdFusion的任何帮助。只是尽量提供尽可能多的信息。

truncate table fix_the_date; commit; 
insert into fix_the_date 
(Location,Invoice,NewDate,TransactionDate) 
values 
('Corporate', '#invoice#'
, to_date('#NewDate#', 'mm/dd/yyyy')
,to_date('#TransactionDate#','mm/dd/yyyy')); 
commit;
<!--- About a dozen other queries go here that I won't waste your time with--->

更新1: 以下是我目前存储过程的内容。我收到以下错误:

  

执行数据库查询时出错。 [Macromedia] [Oracle JDBC   驱动程序] [Oracle] ORA-01858:找到了一个非数字字符   数字是预期的ORA-06512:在“THEDB.FIXMISSINGDATE”,第13行   ORA-06512:第1行错误发生在第4行

CREATE OR REPLACE PROCEDURE THEDB."FIXMISSINGDATE" (Invoice integer, NewTransactionDate date, TransactionDate date) IS
BEGIN

EXECUTE IMMEDIATE 'TRUNCATE TABLE FixTheDate;
commit;

EXECUTE IMMEDIATE 'TRUNCATE TABLE DeletePayTrans';
commit;

insert into FixTheDate(Site, Invoice_No, Pay_Date, Old_Date)
values ('Corporate', 'TO_CHAR(#Invoice#)','#NewTransactionDate#','#TransactionDate#');
commit;

/*******************************
Plus a bunch of other queries here
*******************************/

END FIXMISSINGDATE;
/

更新2:根据评论中的反馈,删除了英镑符号

CREATE OR REPLACE PROCEDURE THEDB."FIXMISSINGDATE" (Invoice integer, NewTransactionDate date, TransactionDate date) IS
BEGIN

EXECUTE IMMEDIATE 'TRUNCATE TABLE FixTheDate;
commit;

EXECUTE IMMEDIATE 'TRUNCATE TABLE DeletePayTrans';
commit;

insert into FixTheDate(Site, Invoice_No, Pay_Date, Old_Date)
values ('Corporate', TO_CHAR(Invoice), NewTransactionDate, TransactionDate);
commit;

/*******************************
Plus a bunch of other queries here
*******************************/

END FIXMISSINGDATE;
/

1 个答案:

答案 0 :(得分:2)

您的问题可能与尝试在存储过程中发出DDL语句有关(请参阅here)。

我将假设您的NewDateTransactionDate也是varchar2(因为如果它们实际上是日期,则您的插入语句将不起作用。)

CREATE OR REPLACE PROCEDURE my_proc(
    invoice         IN integer,
    NewDate         IN varchar2,
    TransactionDate IN varchar2) AS

BEGIN

    EXECUTE IMMEDIATE 'TRUNCATE TABLE fix_the_date';

    INSERT INTO fix_the_date (Location,Invoice,NewDate,TransactionDate) 
    VALUES ('Corporate', invoice, TO_DATE(NewDate, 'mm/dd/yyyy'),
    TO_DATE(TransactionDate,'mm/dd/yyyy')); 
    COMMIT;

END;
/

......或者如果你真的要过日期:

CREATE OR REPLACE PROCEDURE my_proc(
    invoice         IN integer,
    NewDate         IN date,
    TransactionDate IN date) AS

BEGIN

    EXECUTE IMMEDIATE 'TRUNCATE TABLE fix_the_date';

    INSERT INTO fix_the_date (Location,Invoice,NewDate,TransactionDate) 
    VALUES ('Corporate', invoice, NewDate, TransactionDate); 
    COMMIT;

END;
/