Mysql 儲存過程處理動態引數

2021-09-01 16:36:28 字數 908 閱讀 4193

--刪除

drop procedure if exists up_common_select

--建立

create procedure `up_common_select`

(in t_name varchar(50)

)begin

declare v_sql varchar(500);

set v_sql= concat('select * from ',t_name);

select v_sql;

--注意:prepare(預處理)execute stmt using @var,只能跟@var變數,declare和傳入的變數不行!!!

set @v_sql=v_sql;

prepare stmt from @v_sql;

execute stmt ;

deallocate prepare stmt;

end;

--呼叫

call up_common_select('admin_authority');

[size=medium]注意事項[/size]

[size=small]1 mysql5.0.13之後支援在儲存過程中呼叫prepare

2 prepare stmt from 'select * from ?'; (錯)

mysql5.0.24,prepare尚不支援 表名做變數!

解決方案:用 contat()函式,組合字串

3 execute stmt [using @var,@var2]

必須是@var形式的變數,傳入的引數變數,declare變數不行

4. deallocate prepare stmt; 顯式的釋放prepare,如果不釋放,mysql會釋放,![/size]

mysql儲存過程動態引數查詢

1 動態sql,即動態引數 在儲存過程中,想要直接用表名變數做引數,動態執行sql,不能直接寫 12 3456 7createprocedure tablenamechar 20 begin select fromtablename end mysql 不支援表名作為變數,這樣會直接將變數名 tab...

儲存過程動態引數

create or replace procedure testdynamicparams p cmbno in varchar2,p trade date in varchar2 is cursor testcursor is select cmbno,trade date,securno,tur...

mysql儲存過程寫法 動態引數運用

刪除 drop procedure if exists up common select 建立create procedure up common select in t name varchar 50 begin declare v sql varchar 500 set v sql concat...