SQL到存储过程

时间:2017-03-14 14:39:17

标签: oracle stored-procedures

我正在开发一个项目,需要编写存储过程而不是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 个答案:

答案 0 :(得分: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;
/