mysql預存程序寫法—動態參數運用

來源:互聯網
上載者:User

標籤:io   ar   div   sp   代碼   html   on   c   line   

 --刪除

雙擊代碼全選
1 drop procedure if exists up_common_select

 

  --建立

雙擊代碼全選
1234567891011121314 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;

  --調用

雙擊代碼全選
1 call up_common_select(‘admin_authority‘);

 

  注意事項

  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會釋放,!

 

mysql預存程序寫法—動態參數運用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.