mysql 儲存過程和函式的區別

2021-08-03 15:00:26 字數 1113 閱讀 9681

1.函式必須指定返回值,且引數預設為in型別。

2.儲存過程沒返回值,引數可以是 in,out,in out型別,有的人可能會理解成out 也算是返回值。

3.呼叫方式:函式 select my_fun() ;過程 call my_pro( ) ;

demo

delimiter $$

drop function if

exists my_fun$$

create

function my_fun(a int(2),b int(2))

returns int(4)

begin

declare sum_ int(2) default

0;set sum_ = a + b;

return sum_;

end$$

delimiter ;

delimiter $$

drop

procedure

ifexists my_pro$$

create

procedure my_pro(in a int(2),in b int(2) ,out c int(2))

begin

set c = a + b;

end$$

delimiter ;

呼叫 mysql

呼叫

mysql> call my_pro(1,2,@c);

query ok, 0 rows affected (0.00 sec)

mysql> select @c;

+------+

| @c |

+------+

| 3 |

+------+

1 row in set (0.00 sec)

mysql> select my_fun(1,2);

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

| my_fun(1,2) |

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

| 3 |

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

1 row in set (0.00 sec)

MySQL儲存函式和儲存過程的區別

儲存過程與儲存函式的區別 1 儲存函式和儲存過程統稱為儲存例程 store routine 儲存函式的限制比較多,例如不能用臨時表,只能用表變數,而儲存過程的限制較少,儲存過程的實現功能要複雜些,而函式的實現功能針對性比較強 2 返回值不同 儲存函式必須有返回值,且僅返回乙個結果值 儲存過程可以沒有...

MySQL儲存過程和函式的區別

size small mysql的儲存過程 stored procedure 和函式 stored function 統稱為stored routines,是否應該採用儲存過程在文章business logic to store or not to store that is the questio...

mysql 函式和儲存過程的區別

1 一般來說,儲存過程實現的功能要複雜一點,而函式的實現的功能針對性比較強。儲存過程,功能強大,可以執行包括修改表等一系列資料庫操作 使用者定義函式不能用於執行一組修改全域性資料庫狀態的操作。2 對於儲存過程來說可以返回引數,如記錄集,而函式只能返回值或者表物件。函式只能返回乙個變數 而儲存過程可以...