Mysql5 0 儲存過程

2021-04-07 01:11:18 字數 4671 閱讀 4943

stored routines require the proc table in the mysql database. (mysql.proc)

心得:

1.建立儲存過程之前,使用delimiter $$ 來定義語句結束符,建立完成後,還原語句結束符 delimiter ;

由於mysql預設以";"為分隔符,則過程體的每一句都被mysql以儲存過程編譯,則編譯過程會報錯,所以要事先用delimiter關鍵字申明當前段分隔符用完了就把分隔符還原。

2.過程的開始與結束使用begin.....end組合

3.使用show procedure status函式來獲得儲存過程的資訊

儲存過程getcount:

delimiter $$

drop procedure if exists `firstdb`.`getcount` $$

create procedure `getcount`(out totalcount int)

deterministic

begin

select count(*) into totalcount from users;

select  totalcount;

end $$

delimiter ;

call getcount(@count);

儲存過程getcount2:

delimiter $$

drop procedure if exists `firstdb`.`getcount2` $$

create procedure `firstdb`.`getcount2` ()

begin

select count(*) from users;

end $$

delimiter ;

use firstdb;

call getcount2();

函式hellofunc:

delimiter $$

drop function if exists `firstdb`.`hellofunc` $$

create function `firstdb`.`hellofunc` (s char(20)) returns char(50)

begin

return concat('hello, ',s,'!');

end $$

delimiter ;

select hellofunc('king');

mysql5儲存過程編寫實踐***

出自:

mysql5.0以後均支援儲存過程,最近有空,研究了一把這個

格式:

create procedure 過程名 ([過程引數[,...]])

[特性 ...] 過程體

create function 函式名 ([函式引數[,...]])

returns 返回型別

[特性 ...] 函式體

過程引數:

[ in | out | inout ] 引數名 引數型別

函式引數:

引數名 引數型別

返回型別:

有效的mysql資料型別即可

特性:

language sql

| [not] deterministic

| | sql security

| comment 'string'

過程體/函式體:格式如下:

begin

有效的sql語句

end

下面是2個例子,提供了一種字串加密的演算法,每次以相同的入參呼叫都會得到不同的加密結果,

演算法相對比較簡單,不具備強度。分別以函式和過程的形式分別實現如下:

(1)函式

eg:

create function fun_addmm(inpass varchar(10)) returns varchar(11)

begin

declare string_in varchar(39);

declare string_out varchar(78);

declare offset tinyint(2);

declare outpass varchar(30) default ';

declare len tinyint;

/*declare i tinyint;*/

/**/

set len=length(inpass);

if((len<=0) or (len>10)) then

return "";

end if;

set offset=(second(now()) mod 39)+1; /*根據秒數取模*/

/*insert into testtb values(offset,'offset: ');*/

set string_out='yn8k1jozvurb3mdets5gpl27axwihq94c6f0#$_'; /*金鑰*/

set string_in='_$#abcdefghijklmnopqrstuvwxyz0123456789';

set outpass=concat(outpass,substring(string_out,offset,1));

/* insert into testtb values(2,outpass);*/

set string_out=concat(string_out,string_out);

set @i=0;

repeat

set @i=@i+1;

set outpass=concat(outpass,substr(string_out,instr(string_in,substring(inpass,@i,1))+offset,1));

/* insert into testtb values(@i+2,outpass);*/

until (@i>=len)

end repeat;

return outpass;

end

(2)過程

create procedure `pro_addmm`(in inpass varchar(10),out outpass varchar(11))

begin

declare string_in varchar(39);

declare string_out varchar(78);

declare offset tinyint(2);

declare len tinyint;

set outpass=';

set len=length(inpass);

if((len<=0) or (len>10)) then

set outpass=';

else

set offset=(second(now()) mod 39)+1;

set string_out='yn8k1jozvurb3mdets5gpl27axwihq94c6f0#$_';

set string_in='_$#abcdefghijklmnopqrstuvwxyz0123456789';

set outpass=concat(outpass,substring(string_out,offset,1));

set string_out=concat(string_out,string_out);

set @i=0;

repeat

set @i=@i+1;

set outpass=concat(outpass,substr(string_out,instr(string_in,substring(inpass,@i,1))+offset,1));

until (@i>=len)

end repeat;

end if;

end

//執行結果如下:

mysql> call pro_addmm('zhouys',@a);

query ok, 0 rows affected (0.00 sec)

mysql> select @a;

+---------+

| @a |

+---------+

| u_pi6$4 |

+---------+

1 row in set (0.00 sec)

mysql> call pro_addmm('zhouys',@a);

query ok, 0 rows affected (0.00 sec)

mysql> select @a;

+---------+

| @a |

+---------+

| 9p8uegm |

+---------+

1 row in set (0.00 sec)

mysql> select fun_submm('u_pi6$4');

+----------------------+

| fun_submm('u_pi6$4') |

+----------------------+

| zhouys |

+----------------------+

1 row in set (0.00 sec)

加密演算法有幾個弱點:

1、不支援大小寫

2、不支援中文

3、加密強度不夠

Mysql5 0 儲存過程

stored routines require the proc table in the mysql database.mysql.proc 心得 1.建立儲存過程之前,使用delimiter 來定義語句結束符,建立完成後,還原語句結束符 delimiter 由於mysql預設以 為分隔符,則過程...

Mysql5 0 儲存過程

stored routines require the proc table in the mysql database.mysql.proc 心得 1.建立儲存過程之前,使用delimiter 來定義語句結束符,建立完成後,還原語句結束符 delimiter 由於mysql預設以 為分隔符,則過程...

MySQL 建立儲存過程(MySQL 5 0)

mysql 儲存過程是從 mysql 5.0 開始增加的新功能。儲存過程的優點有一籮筐。不過最主要的還是執行效率和sql 封裝。特別是 sql 封裝功能,如果沒有儲存過程,在外部程式訪問資料庫時 例如 php 要組織很多 sql 語句。特別是業務邏輯複雜的時候,一大堆的 sql 和條件夾雜在 php...

MySQL 建立儲存過程(MySQL 5 0)

mysql 儲存過程是從 mysql 5.0 開始增加的新功能。儲存過程的優點有一籮筐。不過最主要的還是執行效率和sql 封裝。特別是 sql 封裝功能,如果沒有儲存過程,在外部程式訪問資料庫時 例如 php 要組織很多 sql 語句。特別是業務邏輯複雜的時候,一大堆的 sql 和條件夾雜在 php...

MySQL 5 0 儲存過程例子(使用了遊標)

begin 給所有人預設 個人 角色 declare done int default 0 declare a varchar 32 declare cur1 cursor for select id from party where party type employee declare cont...