nysql 存储过程案例

案例1 :


DROP PROCEDURE IF EXISTS saveOrUpdate_Adminmenuitems;  ---判断是否存在,存在删除
DELIMITER  //                       --- 定义分割符号,因为mysql里面 ; 符号表示执行的分割符号,而下面代码中要使用到 ; 符号,所以定义分隔符 //
CREATE PROCEDURE saveOrUpdate_Adminmenuitems()
begin 
SELECT count(`itemid`) FROM adminmenuitems where itemname='test_name_new' into @menuitem_id;   
if @menuitem_id<=0 then
INSERT INTO `adminmenuitems` (`itemid`, `itemname`, `itemurl`,`imagename`, `imageurl`,
`description1`,`leftmenuname`, `international`)
select `basekey`+1, 'test_name', 'test.admin',
'test_new','assets/admin/images/preferences.gif',
'test page',
'test_', 0 from `keygenerator` where `name`='`ADMINMENUITEMS`';
update `keygenerator` set `basekey`=`basekey`+1 where `name`='`ADMINMENUITEMS`';
insert into `adminitemsmapping`(`id`,`menuid`,`admintype`,`itemid`,`leftmenu`,`position`,`leftmenuposition`)
select kg.`basekey`+1, am.`menuid`, 1, ami.`itemid`, 1, max(aim.`position`)+1, max(aim.`leftmenuposition`)+1
from `adminmenu` as am, `adminmenuitems` as ami, `keygenerator` as kg, `adminitemsmapping` as aim
where am.`menuname`='Catalog' and ami.`itemname`='test_name' and kg.`name`='`ADMINITEMSMAPPING`' and
am.`menuid` = aim.`menuid`;
update `keygenerator` set `basekey`=`basekey`+1 where `name`='`ADMINITEMSMAPPING`';
end if;
if @menuitem_id>0 then 
update `adminmenuitems` set `itemurl`='managefacetsmanager.admin1111',`leftmenuname`='Manage Facets Manager'
where `itemname`='test_name_new';
end if;
end; 
//
DELIMITER ;              --- 把分隔符号定义回来 继续使用 ; 做分隔符
call saveOrUpdate_Adminmenuitems();
drop procedure saveOrUpdate_Adminmenuitems;
show procedure status;


你可能感兴趣的:(mysql数据库)