mysql存储过程和方法循环等

select * from emp;
select * from dept;
select * from salgrade;


drop PROCEDURE if EXISTS yyk;
create PROCEDURE yyk(
    mingzi VARCHAR(20)
)
BEGIN
select * from emp where ename=mingzi;
END;

call yyk(Tom);


-- 乘法
drop procedure if exists aaa;
create procedure aaa(x int,y int)
BEGIN
    DECLARE z int;
    set z = x*y;
    select z;
END;

call aaa(2,5)


-- 循环
drop procedure if exists bbb;
create procedure bbb(n int)
BEGIN
    DECLARE i int;
    DECLARE r int;
    set i = 1;
    set r = 1;
    while i<= n DO
        set r=r*i;
        set i=i+1;
    END WHILE;
select r;
END;

call bbb(5)

-- --函数 
set global log_bin_trust_function_creators=TRUE;
drop FUNCTION if EXISTS ccc;
create FUNCTION ccc(x int,y int)
RETURNS INT
BEGIN
 DECLARE z int;
 set z=x+y;
 return z;
END;

select ccc(1,2);

-- 触发器
drop TRIGGER if EXISTS tianshao;
create TRIGGER tianshao after update on emp for each ROW
BEGIN
update dept set loc=new.ename where deptno=old.deptno;
END

 

mysql存储过程和方法循环等

上一篇:EFCore中提前将表达式转换成Sql并返回IQueryable 类型的数据避免转换失败


下一篇:SQL关于删除的三个语句:DROP、TRUNCATE、 DELETE 的区别