Oracle过程中的字符串比较

时间:2016-09-21 06:36:55

标签: oracle stored-procedures procedure

尝试比较两个字符串。一个来自IN参数(myname),另一个是硬编码(' abcd')。

(myname IN  VARCHAR2)
-- myname will be set to 'abcd' when calling this procedure
DECLARE
name VARCHAR2(10):=myname;
IF(name='abcd') THEN
--update something in database
END IF;

实际代码如下。 idegreeName设置为' HONS'。如果我删除if块然后我的程序工作正常。但我需要IF块来执行更新查询。

// Calling in JAVA
stmt = (OracleCallableStatement) conn.prepareCall("{ call receiveAdmission2(?,?,?,?,?,?,?,?,?,?,?,?,?,?)  }");
// my procedure
    CREATE OR REPLACE PROCEDURE NU_DB.receiveAdmission2 (

   iAdmissionRoll          IN  VARCHARARRAY,
   iUserId                 IN  VARCHAR2,
   iCollegeCode            IN  VARCHAR2,
   isessionId              IN  VARCHAR2,
   isubjectId              IN  VARCHAR2,
   iMeritType              IN  VARCHAR2,
   iStatus                 IN  VARCHAR2,
   iXforward               IN  VARCHAR2,
   iVia                    IN  VARCHAR2,
   iRemoteAddress          IN  VARCHAR2,

   oResponseCode           OUT NUMBER,
   oResponseMessage        OUT VARCHAR2,
   oResponse               OUT VARCHAR2,

   idegreeName              IN  VARCHAR2

)
IS

 tErrorCode   VARCHAR2(20);
 tErrorMsg    VARCHAR2(400);
 tResponse    VARCHAR2(100);

BEGIN

    oResponse:='';
    DECLARE
    str1  VARCHAR2(10):=idegreeName;

    Begin

        FOR i IN 1 .. iAdmissionRoll.COUNT
        LOOP


           IF(str1='HONS') THEN

           UPDATE nu_hons_result set STATUS=iStatus where ADMISSION_ROLL=iAdmissionRoll(i) and  MERIT_TYPE=iMeritType and COLLEGE_CODE=iCollegeCode and  SUBJECT_ID=isubjectId and STATUS='Student Requested';

           END IF;

         if (sql%rowcount > 0) then
             tResponse := 'Successfully approved.';

             if(iMeritType<>5) then

             UPDATE nu_college_subject_map set AVAILABLE_SEAT=AVAILABLE_SEAT-1 where SESSION_ID=isessionId and COLLEGE_CODE=iCollegeCode and  SUBJECT_ID=isubjectId;

             end if;


            insert into NU_HONS_SUBJECT_ADMISSION (ADMISSION_ROLL,SESSION_ID,COLLEGE_CODE,SUBJECT_ID,APPROVED_BY,APPROVED_ON) 
                values(iAdmissionRoll(i),isessionId,iCollegeCode,isubjectId,iUserId,sysdate);

            insert into nu_log_admission values (iAdmissionRoll(i),isessionId,iCollegeCode,iMeritType,iStatus,sysdate,isubjectId);

           else
             tResponse := 'Failed to approve.'||idegreeName; 
          end if;  

         oResponse := oResponse || tResponse || '###';


        END LOOP;

     EXCEPTION WHEN DUP_VAL_ON_INDEX THEN
        tErrorCode:=SQLCODE;
        tErrorMsg:=SUBSTR(SQLERRM, 1, 400);
        oResponseCode := 1;
        oResponseMessage :=tErrorMsg;
        ROLLBACK;
        Return;
      WHEN OTHERS THEN
        tErrorCode:=SQLCODE;
        tErrorMsg:=SUBSTR(SQLERRM, 1, 400);
        oResponseCode := 2;
        oResponseMessage :=tErrorMsg;
        ROLLBACK;
        Return;

    End;

END receiveAdmission2;
/

问题是IF块没有执行。

1 个答案:

答案 0 :(得分:0)

你的代码就像预期的那样工作。我为测试目的添加了日志记录。显示如何调用代码或添加参数的日志记录并从那里开始工作

DECLARE name VARCHAR2(10);
BEGIN

name := 'abcd';

IF ( name = 'abcd')  THEN
 dbms_output.put_line('equal');
ELSE 
 dbms_output.put_line('NOT equal');
END IF;

END;