2014-03-12 59 views
0

我試圖在Oracle中執行最基本的存儲過程。我只是試圖返回2個查詢由工會加入...代碼如下:Oracle存儲過程中的UNION

CREATE OR REPLACE PROCEDURE GETATMCONTRACTSBYMANAGERID (
    MANAGERID IN NUMBER 
) AS 
BEGIN 
    select t.ID, t."NUMBER", t.TITLE, t.NOTES, t.CONTRACT_TYPE_ID as CONTRACT_TYPE_ID, t.CONTRACT_STATUS_ID as CONTRACT_STATUS_ID, t.TASK_NUMBER as TASK_NUMBER, c.CONSULTANT_ID,t.TASK_MANAGER_ID, t.IS_COMPREHENSIVE, t."NUMBER", TO_DATE(SUBSTR(t."NUMBER", 8, 2), 'rr') as val, SUBSTR(t."NUMBER", 11, 3) as val2, t.REF_ID from CONTRACT t INNER JOIN CONTRACT c on c.ID = t.REF_ID 
    where t.TASK_MANAGER_ID = MANAGERID; 
    UNION 
    select t.ID, t."NUMBER", t.TITLE, t.NOTES, t.CONTRACT_TYPE_ID as CONTRACT_TYPE_ID, t.CONTRACT_STATUS_ID as CONTRACT_STATUS_ID, t.TASK_NUMBER as TASK_NUMBER, t.CONSULTANT_ID, t.TASK_MANAGER_ID, t.IS_COMPREHENSIVE, t."NUMBER", TO_DATE(SUBSTR(t."NUMBER", 8, 2), 'rr') as val, SUBSTR(t."NUMBER", 11, 3) as val2, t.REF_ID from CONTRACT t 
    where t."NUMBER" in (select "NUMBER" from Contract t1 where t1.TASK_MANAGER_ID = MANAGERID) 
    and t.TASK_NUMBER = ' ' 
    order by val desc, val2 desc, TASK_NUMBER asc; 
END GETATMCONTRACTSBYMANAGERID; 

我將是第一個承認我更舒適與SQL Server比我與Oracle但對我來說它似乎應該工作。

它返回的錯誤是:

誤差(7,1):PLS-00103:出現符號「聯盟」在需要下列之一時:(開始情況下宣佈結束退出異常用於轉到如果環模空編譯提高收益選擇更新而與< <繼續關閉當前刪除獲取鎖芯打開回滾保存點設置SQL執行提交FORALL合流管清洗

這個問題的任何幫助,將不勝感激:)

+1

你目前的問題是'UNION'之前的分號。但是,一旦你解決了這個問題,你將會遇到另一個問題,因爲你不使用查詢結果。你想做什麼? –

回答

1

你有union操作之前冗餘分號(;) - 簡單地將其刪除:

select t.ID, t."NUMBER", t.TITLE, t.NOTES, t.CONTRACT_TYPE_ID as CONTRACT_TYPE_ID, t.CONTRACT_STATUS_ID as CONTRACT_STATUS_ID, t.TASK_NUMBER as TASK_NUMBER, c.CONSULTANT_ID,t.TASK_MANAGER_ID, t.IS_COMPREHENSIVE, t."NUMBER", TO_DATE(SUBSTR(t."NUMBER", 8, 2), 'rr') as val, SUBSTR(t."NUMBER", 11, 3) as val2, t.REF_ID from CONTRACT t INNER JOIN CONTRACT c on c.ID = t.REF_ID 
    where t.TASK_MANAGER_ID = MANAGERID -- Semicolon removed here 
    UNION 
    select t.ID, t."NUMBER", t.TITLE, t.NOTES, t.CONTRACT_TYPE_ID as CONTRACT_TYPE_ID, t.CONTRACT_STATUS_ID as CONTRACT_STATUS_ID, t.TASK_NUMBER as TASK_NUMBER, t.CONSULTANT_ID, t.TASK_MANAGER_ID, t.IS_COMPREHENSIVE, t."NUMBER", TO_DATE(SUBSTR(t."NUMBER", 8, 2), 'rr') as val, SUBSTR(t."NUMBER", 11, 3) as val2, t.REF_ID from CONTRACT t 
    where t."NUMBER" in (select "NUMBER" from Contract t1 where t1.TASK_MANAGER_ID = MANAGERID) 
    and t.TASK_NUMBER = ' ' 
    order by val desc, val2 desc, TASK_NUMBER asc; 
+0

這工作,謝謝 – timmyda

1

還有分號的問題,您需要選擇到一個變量,讓您的代碼進行編譯。

+0

我知道這一部分...雖然感謝! – timmyda