Oracle PL/SQL语法混淆?

huangapple go评论84阅读模式
英文:

Oracle plsql syntax confusion?

问题

declare
v_name varchar2(10) := 'bruce';
v_name2 number;

begin
case v_name
when 'bruce' then DBMS_OUTPUT.PUT_LINE('corr');
else DBMS_OUTPUT.PUT_LINE('incorrect');
end case;
DBMS_OUTPUT.PUT_LINE(v_name2);
end;

declare
v_name varchar2(10) := 'bruce';
num number(2) := 10;
begin
num := case v_name
when 'bruce' then 2
else 12
end;
DBMS_OUTPUT.PUT_LINE(num);
end;

Getting confuse about semi colons. why in first code semicolon required and end case; keyword needed. But in 2nd code no semicolon & also just end; I know in 2nd I am assigning value to variable, but so what ?

英文:
declare
    v_name varchar2(10) := 'bruce';
    v_name2 number;

begin
        case v_name
              when 'bruce' then DBMS_OUTPUT.PUT_LINE('corr');
              else DBMS_OUTPUT.PUT_LINE('incorrect');
        end case;
        DBMS_OUTPUT.PUT_LINE(v_name2);
end;




declare
    v_name varchar2(10) := 'bruce';
    num number(2) := 10;
begin
        num := case v_name
              when 'bruce' then 2
              else 12
        end;
        DBMS_OUTPUT.PUT_LINE(num);
end;

Getting confuse about semi colons. why in first code semicolon required and end case; keyword needed. But in 2nd code no semicolon & also just end; I know in 2nd I am assigning value to variable, but so what ?

答案1

得分: 6

case statement vs. case expression.

case statement 用于条件执行 PL/SQL 代码:

    case v_name
          when 'bruce' then DBMS_OUTPUT.PUT_LINE('corr');
          else DBMS_OUTPUT.PUT_LINE('incorrect');
    end case;

case expression 可以在 PL/SQL 和常规 SQL 中使用,返回一个值:

    num := case v_name
           when 'bruce' then 2
           else 12
           end;
英文:

case statement vs. case expression.

The case statement is for conditional execution of PL/SQL code:

    case v_name
          when 'bruce' then DBMS_OUTPUT.PUT_LINE('corr');
          else DBMS_OUTPUT.PUT_LINE('incorrect');
    end case;

The case expression can be used both in PL/SQL and in regular SQL, and returns a value:

    num := case v_name
           when 'bruce' then 2
           else 12
           end;

huangapple
  • 本文由 发表于 2023年8月4日 01:21:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定