zoukankan      html  css  js  c++  java
  • MySQL 变量

    一、变量声明

    MySQL有两种定义变量的方法

    1、DECLARE(存储过程变量)

    定义语句为:

    declare v_name v_type;

    该方法只能在存储过程中(BEGIN...END内)定义局部变量,且会被默认初始化为NULL

    2、SET(会话变量)

    定义语句为:

    set @v_name = 1;
    

    可以在任何地方声明,作为全局变量,需要进行初始化

    二、变量赋值

    MySQL中变量的赋值方法主要有两种:

    1、select ... into [temp]

    2、set [temp] = ...

    三、输出变量

    直接select想要输出的变量名

    set @result = null;
    call proc_calSumByInsWithOut('14365',@result);
    select @result;

    变量的值就会直接输出(注意call调用完存储过程后的分号;)

    下面基于课本上university数据库,用临时变量(DECLARE)实现一个根据教师ID更改对应部门薪水总额的存储过程

    delimiter //
    create procedure proc_calSumSalaryByIns(in i_ID VARCHAR(5))
    begin
        declare d_name VARCHAR(20);
        select dept_name into d_name
        from instructor
        where instructor.ID = i_ID;
    		/*找出id为i_ID教师所在的部门*/
        update department as d set salary_sum = 
    	(select sum(instructor.salary)
    	from instructor
    	where instructor.dept_name = d_name)
        where d.dept_name = d_name;
    end
    //
    delimiter ;
    

      

  • 相关阅读:
    python学习笔记——拾
    python学习笔记——玖
    Python 实现栈与队列
    Vijos1774 机器翻译 [模拟]
    Vijos1788 第K大 [模拟]
    Python 序列求和
    HDU 2102 A计划 DFS与BFS两种写法 [搜索]
    Python 多组输入
    Python 文件读写
    HDU 2068 RPG错排 [错排公式]
  • 原文地址:https://www.cnblogs.com/liziran/p/6113127.html
Copyright © 2011-2022 走看看