您的位置:首页 > 博客中心 > 数据库 >

Oracle 过程中变量赋值

时间:2022-03-10 17:10

create or replace function get_sal1(id employees.employee_id%type)
  return number is
 
  sal employees.salary%type;
 
begin
  sal := 0;
  select salary into sal from employees where employee_id = id;
  return sal;
end;

 

create or replace function get_sal1(id employees.employee_id%type)
  return number is
 
  sal employees.salary%type:= 0;
  --sal := 0;
begin
 
  select salary into sal from employees where employee_id = id;
  return sal;
end;

  

 

 下面会报错:

Compilation errors for FUNCTION HR.GET_SAL1

Error: PLS-00103: 出现符号 "SELECT"在需要下列之一时:         * & = - + ; < / > at in           is mod remainder not rem <an exponent (**)> <> or != or ~= >=           <= <> and or like like2 like4 likec between || multiset           member submultiset        符号 ";" 被替换为 "SELECT" 后继续。 Line: 8 Text: select salary into sal from employees where employee_id = id;

create or replace function get_sal1(id employees.employee_id%type)
  return number is
 
  sal employees.salary%type;
 
begin
  sal := 0
  select salary into sal from employees where employee_id = id;
 
  return sal;
end;

  这个也会报错:

Compilation errors for FUNCTION HR.GET_SAL1

Error: PLS-00103: 出现符号 "="在需要下列之一时:         constant exception           <an identifier> <a double-quoted delimited-identifier> table           long double ref char time timestamp interval date binary           national character nchar        符号 "<an identifier>" 被替换为 "=" 后继续。 Line: 5 Text: sal := 0;

create or replace function get_sal1(id employees.employee_id%type)
  return number is
 
  sal employees.salary%type;
  sal := 0;
 
begin
 
  select salary into sal from employees where employee_id = id;
  return sal;
end;

  

变量名 数据类型[ :=初始值]

语法解析:

SQL> DECLARE

  2       sname VARCHAR2(20) :=‘jerry‘;  ①

  3  BEGIN

  4       sname:=sname||‘ and tom‘;  ②

  5       dbms_output.put_line(sname);  ③

  6  END;

  7  /jerry

PL/SQL procedure successfully completed

代码解析:

 

 

 

 

Oracle 过程中变量赋值,布布扣,bubuko.com

热门排行

今日推荐

热门手游