《Mysql應用mysql存儲過程學習筆記分享》要點:
本文介紹了Mysql應用mysql存儲過程學習筆記分享,希望對您有用。如果有疑問,可以聯系我們。
導讀:本節內容:有關mysql存儲過程的學習筆記本節的內容介紹,以實際的mysql 存儲過程例子基本,配有詳細的注釋,大家注意體會.例子: # 對...
本節內容:
有關mysql存儲過程的學習筆記MYSQL學習
本節的內容介紹,以實際的mysql 存儲過程例子基本,配有詳細的注釋,大家注意體會.MYSQL學習
例子:
?MYSQL學習
# 對于MySQL 5.0.45版本,需要重新定義一個輸入結束標記符,如:delimiter //
# 在所有的存儲過程中使用";"作為每一條語句的結束標記
# 而每一條MySQL語句都必須使用"http://"作為結束標記;
drop procedure if exists phelloworld //
create procedure phelloworld()
begin
select 'Hello,World!' as F;
end;
//
?
drop procedure if exists padd //
create procedure padd
(
a int,
b int
)
?
begin
declare c int;
if a is null then
set a = 0;
end if;
if b is null then
set b = 0;
end if;
set c = a + b;
select c as Sum;
end;
//
?
# 使用mysql變量調用存儲過程
set @a = 10 //
set @b = 12 //
call padd(@a,@b) //
?
# 直接使用常量值調用存儲過程
call padd(11,12) //
?
# 對數據庫表進行操作
# 創建表
drop table if exists mapping //
create table mapping (
`cFieldID` smallint(5) unsigned not null,
`cFieldName` varchar(30) not null,
primary key(`cFieldID`)
)Engine=InnoDB default charset=utf8 //
?
insert into mapping values(1,'MarketValue') //
insert into mapping values(2,'P/L') //
insert into mapping values(3,'EName') //
insert into mapping values(4,'Nominal') //
insert into mapping values(5,'Chg') //
?
# 寫一個存儲過程,往mapping表中插入一條記錄,并返回當前記錄的總數
drop procedure if exists paddmapping //
create procedure paddmapping (
out cnt int
)
begin
declare maxid int;
select max(cFieldID)+1 into maxid from mapping;
insert into mapping values(maxid,'Hello');
select count(cFieldID) into cnt from mapping;
end //
?
# 定義一個包含輸入輸出參數的存儲過程
drop procedure if exists pinoutmapping //
create procedure pinoutmapping (
in cfid int,
out cfnm varchar(30)
)
?
begin
select cFieldName into cfnm from mapping where cFieldID = cfid;
end;
//
?
# 在定義存儲過程時使用輸入參數:in inParam type
# 輸入參數可以在存儲過程中被修改,但是不能返回該輸入參數的值
drop procedure if exists pinparam //
create procedure pinparam (
in inparam int
)
begin
select inparam;
# 在存儲過程中改變輸入參數的值
set inparam = 2;
select inparam;
end;
//
?
# 調用該存儲過程之后再查看輸入參數的值是否發生改變,雖然輸入參數的值在存儲過程中進行了修改
#
?
#在定義存儲過程時使用輸出參數:out outParam type
#輸出參數可以在存儲過程中進行修改,且能返回該輸出參數的值
drop procedure if exists poutparam //
create procedure poutparam (
out outparam int
)
begin
select outparam;
# 改變輸出參數的值
set outparam = 3;
select outparam;
end;
//
?
# 在定義存儲過程時使用inout參數:inout inoutParam type
# 在調用存儲過程時指定參數值,可在存儲過程中改變該參數值且能返回
drop procedure if exists pinoutparam //
create procedure pinoutparam (
inout inoutparam int
)
begin
select inoutparam;
# 在存儲過程中改變inout參數的值
set inoutparam = 3;
# 查看在存儲過程中改變后的參數值
select inoutparam;
end;
//
?
# 在存儲過程中定義變量:declare vname [,vname2 ...] vtype [default value]
# vtype是mysql中的數據類型:int,float,date,varchar(length)
# 變量賦值:set vname=value
drop procedure if exists pvarible //
create procedure pvarible ()
begin
declare uid int(8) default 0;
declare uname varchar(30) default 'root';
select uid as ID,uname as Name;
set uid = 1;
set uname = 'chench';
select uid as ID,uname as Name;
end;
//
?
# 用戶變量
# (1)在MySQL客戶端使用用戶變量
select 'Hello,World!' into @hl //
select @hl //
?
set @gb = 'Good Bye!' //
select @gb //
?
set @gb = 1+2+3 //
select @gb //
?
# (2)在存儲過程中使用用戶變量
drop procedure if exists pgreetingworld //
create procedure pgreetingworld () select concat(@greeting,' World') //
set @greeting = 'Hello' //
call pgreetingworld() //
?
# (3)在存儲過程間調用全局范圍內的變量
set @vglobal="zhangsan" //
drop procedure if exists p1 //
create procedure p1 ()
begin
set @vglobal = 'wangwu'; -- 在存儲過程中改變mysql全部變量的值
select @vglobal; -- 在存儲過程中拜訪mysql全局變量的值
end;
//
?
drop procedure if exists p2 //
create procedure p2 ()
begin
select concat('variable last value is ',@vglobal);
end;
//
?
# 變量作用域
drop procedure if exists p3 //
create procedure p3()
begin
declare x1 varchar(15) default 'outter';
begin
declare x1 varchar(15) default 'inner';
select x1;
end;
select x1;
end;
//
?
# 存儲過程中的條件語句
# 定義一個測試表
drop table if exists pt //
create table pt (
pid int unsigned auto_increment not null,
pname varchar(30) not null,
primary key(pid)
)Engine=InnoDB default charset = utf8 //
?
# (1)if-then -else語句:使用if var then else var end of結構
# 寫一個根據傳遞的參數值是奇數還是偶數來插入數據庫表相應的數據記錄
drop procedure if exists p4 //
create procedure p4 (
in param int
)
begin
declare t int default 0;
set t = param % 2;
if t = 0 then
insert into pt values(null,'event number!');
else
insert into pt values(null,'odd number');
end if;
select * from pt;
end;
//
?
# (2)case語句:使用case var when-then ... end case結構
# 根據傳遞的參數值來插入相應的記錄,往pt表中插入記錄
drop procedure if exists p5 //
create procedure p5 (
in param int
)
begin
declare value int default -1;
set value = param % 2;
case value
when 0 then
insert into pt values(null,'even number');
else
insert into pt values(null,'odd number');
end case;
select * from pt;
end;
//
?
# 存儲過程中的循環語句
# (1)while condition do(先條件再循環)
#...
#? end while
?
# 通過傳遞參數,連續插入新的記錄到pt表中
delimiter //
drop procedure if exists p6 //
create procedure p6 (
in param int
)
begin
declare i int default 0;
if param > 0 then
while i < param do
insert into pt values(null,'param');
set i = i+1;
end while;
end if;
select * from pt;
end;
//
delimiter ;
?
# (2)repeat (先循環再條件)
#...
#until condition
#end repeat
?
# 通過傳遞參數,連續插入新的記錄到pt表中
delimiter //
drop procedure if exists p7 //
create procedure p7 (
in param int
)
begin
declare i int default 0;
if param > 0 then
repeat
insert into pt values(null,'repeat');
set i = i+1;
until i >= param
end repeat;
end if;
select * from pt;
#echo 'successfully!'
end;
//
delimiter ;
?
# (3)loop(沒有初始條件,也沒有結束條件)
#LOOP_LABEL:loop
#...
#leave LOOP_LABEL --離開循環
#end loop --雖然使用leave離開了循環,但是仍然需要使用end loop作為loop的結束標記,這是語法固定結構
?
# 通過參數傳遞,往pt表中插入連續的記錄
delimiter //
drop procedure if exists p8 //
create procedure p8 (
in param int
)
begin
declare i int default 0;
if param > 0 then
LOOP_LABEL:loop
insert into pt values(null,'loop');
set i = i+1;
if i >= param then
leave LOOP_LABEL; #滿足條件就離開循環
end if;
end loop; #雖然已經使用leave離開了循環,但是必須要有loop結束標記
/*while i<param do
insert into pt values(null,'while');
set i=i+1;
end while;
*/
end if;
select * from pt;
end;
//
delimiter ;
?
#
# MySQL存儲過程中使用查詢結果的值來賦值給變量
# 如:select id into id_value from user where id = '';
# 當查詢結果為空時不會進行賦值操作,即:id_value變量將保持原來的值
?
#
查詢數據庫中的存儲過程
?
方法一:
?????? select `name` from mysql.proc where db = 'your_db_name' and `type` = 'PROCEDURE'
?
方法二:
???????? show procedure status;
?
?
查看存儲過程或函數的創建代碼
?
show create procedure proc_name;
show create function func_name;
?
# 統計pt數據表中的記錄總數,在Java程序中調用該存儲過程
delimiter //
drop procedure if exists pcount //
create procedure pcount (
out count int
)
?
begin
#set autocommit = 0;
#start transaction;
select count(pid) into count from pt;
end;
//
delimiter ;
維易PHP培訓學院每天發布《Mysql應用mysql存儲過程學習筆記分享》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/14325.html