mysql函式與儲存過程編寫範例

2022-10-15 02:45:03 字數 3891 閱讀 6223

範例一:

需求:向表t1插入一百萬條資料

drop procedure p16;

delimiter // ---宣告分隔符,mysql預設的分隔符是分號。

create procedure p16()

begin

declare v int;

set v=1;

loop_lable:loop

insert into t1 values(v);

set v=v+1;

if v >=1000000 then

le**e loop_lable;

end if;

end loop;

end;

//delimiter還原分隔符。

執行該過程:

call p16();

範例二:

需求:建立乙個可以生成隨機字串的函式

drop function if exists rand_string;

delimiter //

create function rand_string(l_num tinyint unsigned,l_type tinyint unsigned)

returns varchar(127)

begin

-- function : rand_string

-- author :

-- date : 2015/5/11

-- l_num : the length of random string

-- l_type: the string type

1.0-9

5.0-9a-za-z

-- :

-- mysql> select rand_string(12,5) random_string;

-- | random_string |

-- | 3kzgjcujuplw |

-- 1 row in set (0.00 sec)

declare i int unsigned default 0;

declare v_chars varchar(64) default '0123456789';

declare result varchar ( 255) default '';

if l_type = 1 then

set v_chars = '0123456789';

elseif l_type = 2 then

set v_chars = 'abcdefghijklmnopqrstuvwxyz';

elseif l_type = 3 then

set v_chars = 'abcdefghijklmnopqrstuvwxyz';

elseif l_type = 4 then

set v_chars = '';

elseif l_type = 5 then

set v_chars = '';

else

set v_chars = '0123456789';

end if;

while i < l_num do

set result = concat( result,substr(v_chars,ceil(rand()*(length(v_chars)-1)),1) );

set i = i + 1;

end while;

return result;

end;

//delimiter ;

範例三:

需求:利用上面的隨機函式,建立乙個可以向表中插入隨機資料的儲存過程

drop procedure pro_test;

delimiter //

create procedure pro_test(in v int)

begin

declare i int unsigned default 1;

loop_lable:loop

insert into t1 values(i,rand_string(30,2));

set i=i+1;

if i>=v then

le**e loop_lable;

end if;

end loop;

end;

//delimiter ;

執行該儲存過程讓乙個空表生成1千萬條資料:

call pro_test(10000000);

範例四:

需求:更新一張表,更新之前表的資料如下:

mysql> select * from s1;

| id | address | province | city |

| 1 | 廣東深圳 | null | null |

| 2 | 湖南長沙 | null | null |

| 3 | 湖北武漢 | null | null |

| 4 | 廣西桂林 | null | null |

| 5 | 香港null | null |

| 6 | 美國null | null |

更新之後,表的資料如下:

mysql> select * from s1;

| id | address | province | city |

| 1 | 廣東深圳 | 廣東 | 深圳 |

| 2 | 湖南長沙 | 湖南 | 長沙 |

| 3 | 湖北武漢 | 湖北 | 武漢 |

| 4 | 廣西桂林 | 廣西 | 桂林 |

| 5 | 香港null | null |

| 6 | 美國null | null |

drop procedure pro_splitdata;

delimiter //

create procedure pro_splitdata ()

begin

#declare variable

declare v_id bigint default null;

declare v_address,vv_address varchar(30) default null;

declare done int default false;

#declare cursor

declare cur1 cursor for select id from s1;

#declare handle

declare continue handler for not found set done = true;

#open cursor

open cur1;

#starts the loop

the_loop: loop

fetch cur1 into v_id;

if done then

le**e the_loop;

end if;

set @array_content="廣東廣西湖南湖北";

set @i=1;

#得出陣列成員總數

set @count=char_length(@array_content)-char_length(replace(@array_content1;

while @i <= @count

doset vv_address=substring_index(substring_index(@array_content,' ',@i),' ',-1);

select address into v_address from s1 where id=v_id and address like concat(vv_address,'%');

儲存過程的編寫和呼叫

一.概述 oracle儲存過程開發的要點是 使用notepad文字編輯器,用oracle pl sql程式語言寫乙個儲存過程 在oracle資料庫中建立乙個儲存過程 在oracle資料庫中使用sql plus工具執行儲存過程 在oracle資料庫中修改儲存過程 通過編譯錯誤除錯儲存過程 刪除儲存過程...

儲存過程與觸發器

附頁 例8 1 建立乙個儲存過程,輸出所有學生的姓名,課程名稱和期末成績資訊。create procedure student score asselect sname,cname,final from student s,course c,score sc where and 例8 2 建立乙個儲...

儲存過程與觸發器

1 建立乙個加密過程 建立乙個名稱為p jiami的加密儲存過程,該過程用來查詢一門課程也沒有選修的學生的學號與姓名。最後,執行該儲存過程。create procedure p jiami1 with encryption asselect 學號,姓名 from 學生基本檔案 where 學號not...