2015-11-03 56 views
0

下面是經過3個輸入參數創建功能如何解決ORA-00933:SQL命令在oracle中沒有正確結束?

CREATE OR replace PACKAGE "PKG_CAMPAIGN_EMAIL_QTY" 
AS 
    FUNCTION Getcampaignoutgoingemailqty(
    tablename  IN VARCHAR2, 
    ActivatedDate IN DATE, 
    CompletedDate IN DATE) 
    RETURN NUMBER; 
END pkg_campaign_email_qty; 

/

下面是該查詢得到計數

SELECT 
(
pkg_campaign_email_qty.Getcampaignoutgoingemailqty(
     9142632263013677974, 
     To_date('20/10/2015', 'DD/MM/YYYY'), 
     To_date('30/11/2015', 'DD/MM/YYYY') 
    ) 
) AS 
email 
FROM dual; 

獲取ORA-00933軟件包:SQL命令未在Oracle

正確地結束

這裏是包裝體

CREATE OR REPLACE PACKAGE BODY "PKG_CAMPAIGN_EMAIL_QTY" as 
FUNCTION getCampaignOutgoingEmailQty(tableName IN VARCHAR2 ,ActivatedDate DATE,CompletedDate DATE) RETURN NUMBER IS 
OutgoingEmailQuantity NUMBER; 
    begin 
     EXECUTE IMMEDIATE 'select NVL(COUNT(1),0) from campaign_'||tableName||' 
      join flat_interactions out_email on campaign_'||tableName||'.fullname=out_email.o_parent_id and out_email.N9135700037713613964=9135706250013621563 and out_email.D9135699928113613119 between TO_DATE(''ActivatedDate'',''MM/dd/YYYY'') and TO_DATE(''CompletedDate'',''MM/dd/YYYY'')' INTO OutgoingEmailQuantity; 
RETURN OutgoingEmailQuantity ; 
    EXCEPTION 
     WHEN OTHERS THEN 
     RETURN 0; 
    end getCampaignOutgoingEmailQty; 
end PKG_CAMPAIGN_EMAIL_QTY; 
/
+0

請你可以更新你的問題,以顯示Getcampaignoutgoingemailqty代碼(這將是在包體中)。 – Boneist

+1

我沒有得到運行你的代碼的錯誤。我得到零,因爲你在包函數中壓縮的異常 - 當別人不這樣做時,不要使用'''''''''''''''''''''''' –

+0

我沒有看到使用這麼差的異常處理程序的任何觀點。 –

回答

1

我不知道這是不是你看到的錯誤的原因,但有一個與功能代碼的問題 - 如果你看一下里面的execute眼前,你有

'<snip> and out_email.D9135699928113613119 between TO_DATE(''ActivatedDate'',''MM/dd/YYYY'') and TO_DATE(''CompletedDate'',''MM/dd/YYYY'')' 

這意味着,通過這些參數,你最終會試圖執行的SQL語句:

select NVL(COUNT(1),0) 
from campaign_9142632263013677974 
     join flat_interactions out_email on campaign_9142632263013677974.fullname = out_email.o_parent_id 
              and out_email.N9135700037713613964 = 9135706250013621563 
              and out_email.D9135699928113613119 between TO_DATE('ActivatedDate','MM/dd/YYYY') 
              and TO_DATE('CompletedDate','MM/dd/YYYY'); 

所以,你要的字符串時明確轉換「ActivatedDate」和「CompletedDate」到日期他們不是日期。

不是這樣做,我會使用綁定變量,是這樣的:

create or replace package body pkg_campaign_email_qty 
as 
    function getcampaignoutgoingemailqty(tablename in varchar2 ,activateddate date,completeddate date) 
    return number 
    is 
    outgoingemailquantity number; 
    begin 
    execute immediate 'select NVL(COUNT(1),0)'||chr(10)|| 
         'from campaign_'||tablename||chr(10)|| 
         '  join flat_interactions out_email on campaign_'||tablename||'.fullname=out_email.o_parent_id'||chr(10)|| 
         '           and out_email.N9135700037713613964=9135706250013621563'||chr(10)|| 
         '           and out_email.D9135699928113613119 between :ActivatedDate and :CompletedDate' into outgoingemailquantity using activateddate, completeddate; 
    return outgoingemailquantity; 
    exception 
    when others then 
     return 0; 
    end getcampaignoutgoingemailqty; 
end pkg_campaign_email_qty; 
/

注:未經測試,因爲您沒有提供任何表格定義。


順便說一句,你的電話查詢,外支架是不必要的,我會刪除它們,所以您的查詢就會變成:

SELECT pkg_campaign_email_qty.Getcampaignoutgoingemailqty(9142632263013677974, 
                  To_date('20/10/2015', 'DD/MM/YYYY'), 
                  To_date('30/11/2015', 'DD/MM/YYYY')) email 
FROM dual; 
+0

當你說'#Date From#'時,你的意思是說它是一個參數嗎?什麼會調用這個函數?一些前端代碼(例如C#,Java等)或用戶手動運行它?如果是後者,他們會在哪裏運行它?作爲腳本還是作爲蟾蜍等圖形用戶界面中的臨時聲明? – Boneist

-1
SELECT 
(
    pkg_campaign_email_qty.Getcampaignoutgoingemailqty(9142632263013677974, To_date('20/10/2015', 'DD/MM/YYYY'), To_date('30/11/2015', 'DD/MM/YYYY'))) AS 
    email 
    FROM dual 
); <- missing 
+0

複製查詢時錯過了同樣的問題 – Rahmath

1

您在動態SQL中使用錯誤的日期。

我會用結合爲:

EXECUTE IMMEDIATE 
    'select NVL(COUNT(1),0) 
    from campaign_'||tableName||' 
    join flat_interactions out_email on 
     campaign_'||tableName||'.fullname=out_email.o_parent_id and 
     out_email.N9135700037713613964=9135706250013621563 and 
     out_email.D9135699928113613119 between :ActivatedDate and :CompletedDate' 
    INTO OutgoingEmailQuantity 
    USING ActivatedDate, CompletedDate; 

你想要做什麼是更難遵循:

EXECUTE IMMEDIATE 
    'select NVL(COUNT(1),0) 
    from campaign_'||tableName||' 
    join flat_interactions out_email on 
     campaign_'||tableName||'.fullname=out_email.o_parent_id and 
     out_email.N9135700037713613964=9135706250013621563 and 
     out_email.D9135699928113613119 between 
      TO_DATE('||to_char(ActivatedDate,'MM/dd/YYYY')||',''MM/dd/YYYY'') 
      and 
      TO_DATE('||to_char(CompletedDate,'MM/dd/YYYY')||',''MM/dd/YYYY'')' 
    INTO OutgoingEmailQuantity; 
相關問題