50 7 儲存過程 PROCEDURE

2021-09-22 13:13:52 字數 4474 閱讀 1791

儲存過程沒有返回資料,需使用call proc()呼叫

create definer=`neo`@`%` procedure `angelfund`(in `puid` varchar(50), in `ptime` datetime)

language sql

not deterministic

contains sql

sql security definer

comment ''

begin

declare fusername varchar(16) default null;

declare fname varchar(16) default null;

declare fmembers_date varchar(20) default null;

select username,name,from_unixtime(createtime) into fusername,fname,fmembers_date from members where username = puid;

if fusername is not null then

insert ignore into angelfund(username,name,members_date,accounts_date,endtime,`status`,op,operator,`description`) value(fusername,fname,fmembers_date,ptime,date_add(ptime, interval +1 month),'n','n','computer','');

end if;

end

呼叫過程

call angelfund('100','2013-10-10 10:10:10');

在過程中執行sql,下面的例子是檔案匯出的例子。

drop procedure if exists `export_file`;

delimiter $$

create definer=`dba`@`%` procedure `export_file`(in file_name char(64), in tabname char(64))

begin

set @sql = concat('select * into outfile ',"'/var/lib/mysql-files/",file_name,"'",' from ', tabname) ;

-- select @sql;

prepare stmt from @sql;

execute stmt;

deallocate prepare stmt;

end$$

delimiter ;

call 儲存過程

call test.export_file('test', 'mytable');

mysql> prepare stmt1 from 'select sqrt(pow(?,2) + pow(?,2)) as hypotenuse';

query ok, 0 rows affected (0.00 sec)

statement prepared

mysql> set @a = 3;

query ok, 0 rows affected (0.00 sec)

mysql> set @b = 4;

query ok, 0 rows affected (0.00 sec)

mysql> execute stmt1 using @a, @b;

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

| hypotenuse |

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

| 5 |

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

1 row in set (0.00 sec)

mysql> deallocate prepare stmt1;

query ok, 0 rows affected (0.00 sec)

mysql>

use `test`;

drop procedure if exists `test`;

delimiter $$

use `test`$$

create definer=`dba`@`%` procedure `test`(in a int, in b int ,out num int)

begin

set num = a + b;

end$$

delimiter ;

執行後返回結果 10

set @num = 0;

call test(3,7,@num);

select @num;

use `netkiller`;

drop procedure if exists `table2json`;

delimiter $$

use `netkiller`$$

create definer=`neo`@`%` procedure `table2json`(

in `schema` varchar(32),

in `table` varchar(32),

in `id` varchar(10),

out rev varchar(1024)

)begin

set @column = null;

set @str = null;

select

group_concat(fields) as col into @column from (

select

concat('"', column_name, '",', column_name) as fields

from

information_schema.columns

where

table_name = `table`

and table_schema = `schema`) as tmptable;

-- select @column;

set @sql = concat('select json_object(',@column, ' ) as json into @str from ', `table`,' where id = ', `id`);

-- select @sql;

prepare stmt from @sql;

execute stmt;

deallocate prepare stmt;

set rev = @str;

end$$

delimiter ;

使用例項

set @rev = '0';

call netkiller.table2json('test', 'test', '1', @rev);

select @rev;

use `netkiller`;

drop procedure if exists `trigger2json`;

delimiter $$

use `netkiller`$$

create definer=`root`@`localhost` procedure `trigger2json`(

in `schema` varchar(32),

in `table` varchar(32),

out rev varchar(1024)

)begin

set @column = null;

set @str = null;

select

group_concat(fields) as col

into @column from

(select

concat('"', column_name, '", new.', column_name) as fields

from

information_schema.columns

where

table_name = `table`

and table_schema = `schema`) as tmptable;

-- select @column;

set @sql = concat('select json_object(',@column, ' ) as json into @str ');

-- select @sql;

prepare stmt from @sql;

execute stmt;

deallocate prepare stmt;

set rev = @str;

end$$

delimiter ;

set @rev = '0';

call neo.trigger2json('gw', 'member', @rev);

select @rev;

儲存過程系列之儲存過程sql查詢儲存過程的使用

1.查詢某個表被哪些儲存過程 以下簡稱 sp 使用到 select distinct object name id from syscomments where id in select object id from sys.objects where type p and text like ta...

儲存過程系列之儲存過程sql查詢儲存過程的使用

1.查詢某個表被哪些儲存過程 以下簡稱 sp 使用到 select distinct object name id from syscomments where id in select object id from sys.objects where type p and text like ta...

Oracle儲存過程呼叫儲存過程

oracle儲存過程呼叫有返回結果集的儲存過程一般用光標的方式,宣告乙個游標,把結果集放到游標裡面,然後迴圈游標 declare newcs sys refcursor cs1 number cs2 number cstype table rowtype table列的個數和newcs返回的個數一樣...