-- 不行的代码:Not allowed to return a result set from a function create function myf()returns int begin select * from student; return 100; end;
create function 函数名([参数列表]) returns 数据类型 begin sql语句; return 值; end;
-- 最简单的仅有一条sql的函数 create function myselect2() returns int return 666; select myselect2(); -- 调用函数 -- create function myselect3() returns int begin declare c int; select id from class where cname="python" into c; return c; end; select myselect3(); -- 带传参的函数 create function myselect5(name varchar(15)) returns int begin declare c int; select id from class where cname=name into c; return c; end; select myselect5("python");
-- 无参调用 select myselect3(); -- 传参调用 select myselect5("python"); select * from class where id=myselect5("python");