赞
踩
delimiter $
CREATE PROCEDURE 存储过程名()
begin
存储过程体
end $;
delimiter $
CREATE PROCEDURE 存储过程名(in|out|inout 参数名1 参数类型,参数名2 参数类型……)
begin
存储过程体
end $;
DROP PROCEDURE IF EXISTS `存储过程名`;
delimiter $$
$$
是分隔符,用其他符号也行,比如一个$
或者//
等DECLARE
DECLARE `de_test` VARCHAR(20) DEFAULT '';
@符号
SET
直接赋值变量,变量名以 @
开头:如:set @dogNum = 1002;
prepare
语法格式prepare stmt from 'sql语句; --定义
execute stmt; -- 执行
deallocate prepare stmt; -- 删除定义(释放资源)
DROP PROCEDURE IF EXISTS `sp_select_one_age_dogs`;
delimiter $
CREATE PROCEDURE sp_select_one_age_dogs()
begin
select * from dog d where d.dog_age <=1;
end $
call sp_select_one_age_dogs();
DROP PROCEDURE IF EXISTS `sp_test`;
delimiter $
CREATE PROCEDURE sp_test()
begin
DECLARE `col_test` VARCHAR(20) DEFAULT '';
select 'test' into col_test from dual;
select col_test;
end $;
call sp_test();
DROP PROCEDURE IF EXISTS `sp_select_dog_by_num`;
delimiter $
CREATE PROCEDURE sp_select_dog_by_num(in dogNum int(10))
begin
select d.dog_num ,d.dog_name ,d.dog_kind ,d.dog_age
from dog d where d.dog_num =dogNum;
end $
call sp_select_dog_by_num(1001);
set @dogNum = 1002;
call sp_select_dog_by_num(@dogNum);
set @dogNum := 1002;
DROP PROCEDURE IF EXISTS `sp_test_out`;
delimiter $
CREATE PROCEDURE sp_test_out(out col_test varchar(20))
begin
select 'test' into col_test from dual;
end $;
@
call sp_test_out(@col_test);
select @col_test;
delimiter $
CREATE PROCEDURE sp_select_dogName_by_num(in dogNum int(10),out dogName varchar(20))
begin
select d.dog_name into dogName from dog d where d.dog_num =dogNum;
end $
set @dogNum := 1003;
call sp_select_dogName_by_num(@dogNum,@dogName);
select @dogName;
DROP PROCEDURE IF EXISTS `sp_select_pId_by_deptId`;
delimiter $
CREATE PROCEDURE sp_select_pId_by_deptId(inout v_code varchar(10))
begin
SELECT t.PARENT_ID into v_code FROM SYS_COMPANY_DEPT t
where t.DEPT_ID =v_code;
end $
delimiter ;
set @code := 'C001';
call sp_select_pId_by_deptId(@code);
select @code;
create_time
和 update_time
drop procedure if exists `add_col_date`; delimiter $$ create procedure add_col_date(in tableName varchar(50)) begin set @tableName = tableName; set @createTimeSql = concat(' alter table ',@tableName,' add create_time datetime;'); set @updateTimeSql = concat(' alter table ',@tableName,' add update_time datetime;'); select @createTimeSql; prepare stmt from @createTimeSql; prepare stmt2 from @updateTimeSql; execute stmt; execute stmt2; deallocate prepare stmt; -- 释放数据库连接 deallocate prepare stmt2; end $$ delimiter ;
drop procedure if exists sp_find_pId_by_deptId;
delimiter $$
create procedure sp_find_pId_by_deptId(inout deptId varchar(10))
begin
declare count_num int(10);
SET @@max_sp_recursion_depth = 10;
select count(0) into count_num from sys_company_dept t where t.`TYPE` ='1' and t.dept_id =deptId;
if (count_num = 0) then
select t.PARENT_ID into deptId from sys_company_dept t where t.dept_id =deptId;
call sp_find_pId_by_deptId(deptId);
end if;
end $$
delimiter ;
set @deptId:='D001';
call sp_find_pid_by_deptId(@deptId);
select @deptId;
call sp_find_pid_by_deptId(@deptId)
1456 - Recursive limit 0 (as set by the max_sp_recursion_depth variable) was exceeded for routine sp_find_pid_by_deptId
select @@max_sp_recursion_depth;
SET @@max_sp_recursion_depth = 10;
delimiter $$
#在函数名后面一定要加上returns 函数返回类型
create function fun_get_dog_name(dogNum VARCHAR(10))
returns VARCHAR(30)
begin
declare dogName VARCHAR(30); #在函数中定义一个变量,用来接收函数返回值
函数逻辑处理
return dogName; # 返回变量
end $$
drop function if exists 函数名;
drop function if exists fun_get_dog_name;
delimiter $$
#在函数名后面一定要加上returns 函数返回类型
create function fun_get_dog_name(dogNum VARCHAR(10)) returns VARCHAR(30)
begin
declare dogName VARCHAR(30); #在函数中定义一个变量,用来接收函数返回值
select d.dog_name into dogName from dog d where d.dog_num =dogNum;
return dogName;
end $$
select fun_get_dog_name('1001');
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。