2017-03-14 50 views
0

我正在開發一個項目,並且需要編寫存儲過程而不是SQL查詢。我從來沒有這樣做過,我現在通過將寫入的sql轉換爲過程來嘗試。但是,我無法獲得這個錯誤並且無法正常工作。從你們這些人的任何建議是非常有幫助的解決這個..SQL到存儲過程

SQL:

create or replace 
PROCEDURE MS_TST_PROC AS 

BEGIN 

DECLARE 
    l_organization varchar2(40); 
    l_framework varchar2(10); 
    l_sub_category_code varchar2(20); 
    l_sub_category varchar2(20); 
    l_TST_function varchar2(20); 
    l_questionnaire_name varchar2(20); 
    l_responded_on varchar2(20); 
    l_overall_score number(10); 
    l_target_score number(10); 
    l_maturity number(10,2); 
    l_full_name varchar2(20); 

    cursor c_get_details 
is 

    select 
    ts.organization_name, 
    q.framework, 
    q.sub_category_code, 
    q.sub_category, 
    tst.tst_function, 
    q.questionnaire_name, 
    resp.responded_on , 
    resp.overall_score, 
    ts.target_score, 
    Round((resp.overall_score/ts.target_score)*100,2) as Maturity, 
    users.first_name || ' ' || users.last_name as full_name 
    into 
    l_organization, 
    l_framework, 
    l_sub_category_code, 
    l_sub_category, 
    l_tst_function, 
    l_questionnaire_name, 
    l_responded_on, 
    l_overall_score, 
    l_target_score, 
    l_maturity, 
    l_full_name 

from MS_CMM_QUESTIONNAIRE q 
INNER JOIN MS_CMM_TARGET_SCORE ts 
on q.sub_category_code = ts.sub_category_code 
INNER JOIN MS_CMM_CSF_FUNCTIONS tst 
on tst.sub_category_code = q.sub_category_code 
INNER JOIN MS_QSM_QUESTIONNAIRE qsm 
on q.QUESTIONNAIRE_NAME = qsm.QUE_NAME 
INNER JOIN MS_QSM_QUESTNR_RESP resp 
on resp.QUESTIONNAIRE_ID = qsm.QUE_ID 
and resp.applies_to_object = ts.organization_name 
INNER JOIN SI_USERS_T users 
on users.user_name = resp.respondent; 

END MS_TST_PROC; 

和編譯錯誤說:

Error(60,1): PLS-00103: Encountered the symbol "END" when expecting one of the following:  begin function pragma procedure subtype type <an identifier> <a double-quoted delimited-identifier> current cursor delete exists prior 
+1

你確定你是使用SQL Server的工作?在sql server中沒有創建或替換(還)。而你的錯誤信息來自Oracle。 –

+0

對不起,你說的對,讓我改變我的標籤。 – samuelebe

+0

@SeanLange我在想同樣的事情。 – Mike

回答

3

有幾個問題:

  • 你有一個DECLARE聲明沒有後續BEGINEND聲明。
  • 您有CURSORINTO條款;他們不能都在那裏。如果查詢返回一行,那麼只需使用SELECT ... INTO ...(如下所示),否則,如果您有多行需要處理,則可以使用遊標循環。

而且,它是非常容易閱讀(並找到無與倫比DECLARE/BEGIN/END語句),如果你格式化你的代碼,並保持縮進的適當水平。

事情是這樣的:

CREATE PROCEDURE MS_TST_PROC 
AS 
    l_organization  MS_CMM_TARGET_SCORE.ORGANIZATION%TYPE; 
    l_framework   MS_CMM_QUESTIONNAIRE.FRAMEWORK%TYPE; 
    l_sub_category_code MS_CMM_QUESTIONNAIRE.SUB_CATEGORY_CODE%TYPE; 
    l_sub_category  MS_CMM_QUESTIONNAIRE.SUB_CATEGORY%TYPE; 
    l_TST_function  MS_CMM_CSF_FUNCTIONS.TST_FUNCTION%TYPE; 
    l_questionnaire_name MS_CMM_QUESTIONNAIRE.QUESTIONNAIRE_NAME%TYPE; 
    l_responded_on  MS_QSM_QUESTNR_RESP.RESPONDED_ON%TYPE; 
    l_overall_score  MS_QSM_QUESTNR_RESP.OVERALL_SCORE%TYPE; 
    l_target_score  MS_CMM_TARGET_SCORE.TARGET_SCORE%TYPE; 
    l_maturity   number(10,2); 
    l_full_name   varchar2(20); 
BEGIN  
    SELECT ts.organization_name, 
     q.framework, 
     q.sub_category_code, 
     q.sub_category, 
     tst.tst_function, 
     q.questionnaire_name, 
     resp.responded_on, 
     resp.overall_score, 
     ts.target_score, 
     Round((resp.overall_score/ts.target_score)*100,2), 
     users.first_name || ' ' || users.last_name 
    INTO l_organization, 
     l_framework, 
     l_sub_category_code, 
     l_sub_category, 
     l_tst_function, 
     l_questionnaire_name, 
     l_responded_on, 
     l_overall_score, 
     l_target_score, 
     l_maturity, 
     l_full_name 
    FROM MS_CMM_QUESTIONNAIRE q 
     INNER JOIN MS_CMM_TARGET_SCORE ts 
     on q.sub_category_code = ts.sub_category_code 
     INNER JOIN MS_CMM_CSF_FUNCTIONS tst 
     on tst.sub_category_code = q.sub_category_code 
     INNER JOIN MS_QSM_QUESTIONNAIRE qsm 
     on q.QUESTIONNAIRE_NAME = qsm.QUE_NAME 
     INNER JOIN MS_QSM_QUESTNR_RESP resp 
     on resp.QUESTIONNAIRE_ID = qsm.QUE_ID 
      and resp.applies_to_object = ts.organization_name 
     INNER JOIN SI_USERS_T users 
     on users.user_name = resp.respondent; 

    -- Do something with the values. 
END MS_TST_PROC; 
/
+0

其他問題:處理具有多行的結果集(極有可能);傳入參數來限制結果集。 – APC

+0

感謝MT0,我遵循正確的縮進並能夠成功編譯它。我將在輸出中處理多行。你認爲我應該使用遊標來遍歷所有結果嗎? – samuelebe

+0

@samuelebe如果你要處理多行,那麼你需要一個遊標循環或者使用'BULK COLLECT INTO'和集合。 – MT0