1、书写存储过程
DROP PROCEDURE IF EXISTS count_has_same_name_proc; CREATE PROCEDURE count_has_same_name_proc (xname VARCHAR(50 ) ) BEGIN SELECT COUNT(* ) FROM t_user WHERE name = xname; END;
2、函数书写执行
错误写法
DROP FUNCTION IF EXISTS count_has_same_name_func; CREATE FUNCTION count_has_same_name_func ( xname VARCHAR(50 ) ) RETURNS INT BEGIN SELECT COUNT(id ) FROM t_user WHERE name = xname; END;
正确写法
DROP FUNCTION IF EXISTS count_has_same_name_func; CREATE FUNCTION count_has_same_name_func ( xname VARCHAR(50 ) ) RETURNS INT BEGIN DECLARE s INT ; SELECT COUNT(id ) into s FROM t_user WHERE name = xname; RETURN s; END;
注:trigger和function都需要写成 select ...into 这种句式,否者会报1415错误。