存储过程的使用方法案例


delimiter $$
CREATE PROCEDURE hello_procedure()
BEGIN
SELECT 'hello procedure';
END $$

call hello_procedure


DELIMITER $$

CREATE PROCEDURE King()
BEGIN
DECLARE Kings VARCHAR(20) DEFAULT 'SF';
SET Kings='UG';
SELECT Kings;
END$$

DROP PROCEDURE King

 

 


delimiter $$
CREATE PROCEDURE King()
BEGIN
SELECT * INTO @xlsd FROM users a;
SELECT @xlsd;
END$$


CALL king

DROP PROCEDURE King


delimiter $$
create procedure likes()
begin
DECLARE c INT;
DECLARE a INT;
DECLARE b INT;
set a=1;
set b=1;
set c=a + b;
select c as SUM;
END$$
CALL likes


/* 入参*/
delimiter $$
CREATE PROCEDURE sp_param01(IN nickname VARCHAR(32))
BEGIN
SET @user_age=nickname;
SELECT @user_age AS 姓名;
END $$

CALL sp_param01('林新')

SELECT @user_age$$

DROP PROCEDURE sp_param01

 

/*出参和入参*/
delimiter $$
CREATE PROCEDURE sp_param01(IN nickname VARCHAR(32),OUT NAME VARCHAR(32))
BEGIN
SELECT a.uid INTO NAME FROM users a WHERE a.uname=nickname;
SELECT NAME;
END $$

CALL sp_param01("陈小",@NAME)

 

 

SELECT * FROM users

DROP PROCEDURE sp_param01

 

 

/*存储过程中if的使用*/
delimiter $$
CREATE PROCEDURE sp_param01(IN King VARCHAR(32),OUT result VARCHAR(32))
BEGIN
if King>0 then
SET result='大哥';
ELSEIF King>2 then
SET result='弟弟';
ELSE
SET result='大姐';
end if;

SELECT result;
END $$

CALL sp_param01("-1",@result)

DROP PROCEDURE sp_param01

 

posted @ 2021-05-10 18:52  林新i  阅读(51)  评论(0编辑  收藏  举报