简体   繁体   English

IN参数初始化中使用的UPPER函数未按预期工作

[英]UPPER function used in initialisation of an IN parameter not working as expected

CREATE OR REPLACE PROCEDURE appraisal
  (p_grade IN VARCHAR2 := UPPER(' '))
IS
 v_appraisal VARCHAR2(20) := '';
BEGIN
 v_appraisal := CASE p_grade
     WHEN 'A' THEN 'Excellent'
     WHEN 'B' THEN 'Very good'
     WHEN 'C' THEN 'Bad'
     ELSE 'No such grade!'
     END;
 DBMS_OUTPUT.PUT_LINE('Grade:-'||p_grade ||' Appraisal:-'|| v_appraisal);
END;
/

EXECUTE appraisal('a');

Output: 输出:

Grade:-a Appraisal:-No such grade!"

I am wondering why is this not working - what i am doing wrong? 我想知道为什么这不起作用-我做错了什么?

The UPPER() function does return the uppercase version of whatever is passed to it, but in your default clause you're passing the literal ' ' , which is a single space. UPPER()函数确实返回传递给它的所有内容的大写版本,但是在默认子句中,您传递的是文字' ' ,它是一个空格。 Whitespace has no casing so there is no difference between lowercase space and uppercase space - the concept doesn't really make sense. 空格没有大小写,因此小写和大写之间没有区别-这个概念没有任何意义。 You are not passing in the procedure's parameter value to that function. 没有将过程的参数值传递给该函数。

The defaut clause is there to provide a default value if the caller doesn't supply one. 如果调用方不提供默认值,则defaut子句可提供默认值。 So if you did execute appraisal; 因此,如果您确实execute appraisal; then within that procedure call the p_grade variable would have a value if a single space, which isn't helpful, and isn't what you want here. 然后在该过程中调用p_grade变量(如果有一个空格)将具有一个值,这无济于事,也不是您想要的。 You don't really want a default at all. 您真的根本不需要默认值。

As @DavidFaber said you need to get the uppercase equivalent of the parmeter value when you evaluate it, so you could do: 就像@DavidFaber所说的那样,您需要在评估它时获得与参数值相等的大写字母,因此您可以执行以下操作:

CREATE OR REPLACE PROCEDURE appraisal
  (p_grade IN VARCHAR2)
IS
 v_appraisal VARCHAR2(20);
BEGIN
 v_appraisal := CASE UPPER(p_grade)
     WHEN 'A' THEN 'Excellent'
     WHEN 'B' THEN 'Very good'
     WHEN 'C' THEN 'Bad'
     ELSE 'No such grade!'
     END;
 DBMS_OUTPUT.PUT_LINE('Grade:-'|| UPPER(p_grade)
  ||' Appraisal:-'|| v_appraisal);
END;
/

Alternativley you could declare a local variable that is set to the uppercase value and use that: 您可以声明Alternativley的局部变量,该变量设置为大写值并使用:

 v_grade varchar2(1) := UPPER(p_grade);

You shouldn't generally assume that whoever calls your procedure will display the dbms_output buffer. 通常,您不应假定任何调用您的过程的人都会显示dbms_output缓冲区。 It's OK for debugging or experimenting where you have control of the calling environment, but not in real code, usually. 可以在可以控制调用环境的地方进行调试或试验,但通常不能在实际代码中进行。 You might actually want a function that returns the v_appraisal values, for example. 例如,您实际上可能需要一个返回v_appraisal值的函数。 Here's an SQL Fiddle demo of a simole function version . 这是simole函数版本的SQL Fiddle演示 Or just a look-up table that holds the grades and their descriptions. 或者只是一个查找表,其中包含成绩及其说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM