您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL 存储过程的创建方案

2024/3/2 15:19:17发布15次查看
以下的文章主要介绍的是mysql 存储过程的实际操作解析,我们大家都知道一个mysql 存储过程主要包括名字,参数列表,还有包括很多的sql语句与sql语句集。以下就是具体内容的描述, 创建mysql 存储过程: 语法: createprocedurep() begin /*此存储过程的正文*
以下的文章主要介绍的是mysql 存储过程的实际操作解析,我们大家都知道一个mysql 存储过程主要包括名字,参数列表,还有包括很多的sql语句与sql语句集。以下就是具体内容的描述,
创建mysql 存储过程:
语法:
create procedure p()  begin 
/*此存储过程的正文*/
end  create procedure productpricing()  begin  select avg(pro_price) as priceaverage  from products;  end;   
begin…end之间是存储过程的主体定义
mysql的分界符是分号(;)
调用存储过程的方法是:
call加上过程名以及一个括号
例如调用上面定义的mysql 存储过程
call productpricing();  
哪怕是不用传递参数,存储过程名字后面的括号“()”也是必须的
删除存储过程的方法是:
drop procudure productpricing; 
创建带参数的存储过程:
create procudure productpricing(  out p1 decimal(8,2),  out ph decimal(8,2),  out pa decimal(8,2)  )  begin  select min(prod_price) into pl from products;  select max(prod_price) into ph from products;   select avg(prod_price) into pa from products;  end;   
decimal用于指定参数的数据类型
out用于表明此值是用于从存储过程里输出的
mysql支持 out, in, inout
调用带参数的mysql 存储过程:
call productpricing(@pricelow,  @pricehigh,  @priceaverage);  
所有的参数必须以@开头
要想获取@priceaverage的值,用以下语句
select @priceaverage;
获取三个的值,用以下语句
select @pricehigh, @pricelow, @priceaverage; 
另一个带in和out参数的存储过程:
create procedure ordertotal(  in onumber int,  out ototal decimal(8,2)  )  begin  select sum(item_price*quantity)  from orderitems  where order_num = onumber into ototal;  end;  call ordertotal(20005, @total);  select @total; 
添加一个完整的例子:(这是一个自定义分页的mysql 存储过程)
delimiter $$  drop procedure if exists `dbcall`.`get_page`$$  create definer=`root`@`localhost` procedure `get_page`(  /**//*table name*/  tablename varchar(100),  /**//*fileds to display*/  fieldsnames varchar(100),  /**//*page index*/  pageindex int,  /**//*page size*/  pagesize int,   /**//*field to sort*/  sortname varchar(500),  /**//*condition*/  strwhere varchar(500)  )  begin   declare fieldlist varchar(200);   if fieldsnames=''||fieldsnames=null then  set fieldlist='*';  else  set fieldlist=fieldsnames;   end if;  if strwhere=''||strwhere=null then  if sortname=''||sortname=null then   set @strsql=concat('select ',fieldlist,' from ',tablename,' limit ',(pageindex-1)*pagesize,',',pagesize);  else  set @strsql=concat('select ',fieldlist,' from ',tablename,' order by ',sortname,' limit ',(pageindex-1)*pagesize,',',pagesize);   end if;  else  if sortname=''||sortname=null then  set @strsql=concat('select ',fieldlist,' from ',tablename,' where ',strwhere,' limit ',(pageindex-1)*pagesize,',',pagesize);  else  set @strsql=concat('select ',fieldlist,' from ',tablename,' where ',strwhere,' order by ',sortname,' limit ',(pageindex-1)*pagesize,',',pagesize);   end if;  end if;   prepare stmt1 from @strsql;   execute stmt1;  deallocate prepare stmt1;  end$$  delimiter ;  
以上的相关内容就是对mysql 存储过程的介绍,望你能有所收获。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product