zoukankan      html  css  js  c++  java
  • Oracle 过程中变量赋值

    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;
    

      

    转:

    PL/SQL支持SQL中的数据类型,PL/SQL中正常支持NUMBER,VARCHAR2,DATEOracle SQL数据类型。声明变量必须指明变量的数据类型,也可以声明变量时对变量初始化,变量声明必须在声明部分。声明变量的语法是:

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

    语法解析:

    数据类型如果需要长度,可以用括号指明长度,比如:varchar2(20)

     

    声明变量

    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

    代码解析:

         声明一个变量sname,初始化值是“jerry”。字符串用单引号,如果字符串中出现单引号可以使用两个单引号(’’)来表示,即单引号同时也具有转义的作用。     对变量sname重新赋值,赋值运算符是“:=”。

         dbms_output.put_line是输出语句,可以把一个变量的值输出,在SQL*Plus中输出数据时,可能没有结果显示,可以使用命令:set serveroutput on设置输出到SQL*Plus控制台上。

     

     

    对变量赋值还可以使用SELECT…INTO 语句从数据库中查询数据对变量进行赋值。但是查询的结果只能是一行记录,不能是零行或者多行记录

     

     

    数据库赋值
      数据库赋值是通过 SELECT 语句来完成的,每次执行 SELECT 语句就赋值一次,一般要求被赋值的变量与SELECT中的列名要一一对应。如:
    例:
    DECLARE
    emp_id    emp.empno%TYPE :=7788;
    emp_name  emp.ename%TYPE;
    wages     emp.sal%TYPE;
    BEGIN
    SELECT ename, NVL(sal,0) + NVL(comm,0) INTO emp_name, wages
    FROM emp WHERE empno = emp_id;
    Dbms_output.put_line(emp_name||'----'||to_char(wages));
    END;
     
    提示:不能将SELECT语句中的列赋值给布尔变量。

     

     

    字符及数字运算特点
    空值加数字仍是空值:NULL + < 数字> = NULL
    空值加(连接)字符,结果为字符:NULL || <字符串> = < 字符串>

     
  • 相关阅读:
    Linux-RedHat 手动创建RAID和LVM分区
    Centos 文件系统 xfs、ext4、ext3 的区别
    CentOS7.5 rpm方式安装MySQL8.0.13
    virtualbox-host-only模式主机能上网虚拟机无法上网的问题解决
    RPA-智能流程自动化解决方案
    论文笔记——Rethinking the Inception Architecture for Computer Vision
    论文笔记——Factorized Convolutional Neural Networks
    论文笔记—Flattened convolution neural networks for feedforward acceleration
    论文笔记——Data-free Parameter Pruning for Deep Neural Networks
    AlexNet网络结构特点总结
  • 原文地址:https://www.cnblogs.com/softidea/p/3724227.html
Copyright © 2011-2022 走看看