在mysql上使用儲存過程給相應的表加入字段

2021-08-02 21:39:26 字數 1338 閱讀 1561

程式公升級需要給mysql的資料庫加上特定欄位以供公升級需要

create

procedure thams.adds()

begin

#定義變數

declare _tablename varchar(32);

declare _done int default 0;

#將查詢的結果賦予變數cur

declare _cur cursor for

select table_name from information_schema.`columns`

where table_schema='thams'

and table_name like

'%_document'

group

by table_name ;

declare continue handler

forsqlstate

'02000'

set _done=1;#錯誤定義,標記迴圈結束

#開啟游標cur

open _cur;

#有條件控制的迴圈語句。當滿足特定條件until時,就會跳出迴圈語句

repeat

#使用游標

fetch _cur into _tablename;

if not _done then

begin

ifnot

exists (select * from information_schema.columns where table_schema = 'thams'

and table_name = _tablename and column_name = 'expireyear') then

set @stmt := concat("alter table ",_tablename," add column expireyear varchar(30);");

prepare stmt from @stmt;

execute stmt;

#alter

table _tablename add

column project_id varchar(32);

endif;

end;

endif;

until _done end repeat;

#關閉游標

close _cur;

set _done = 0;#只有定義為0,新的迴圈才能繼續。

end;

call thams.adds();

Mysql儲存過程使用

案例 create procedure sp insert graduate certificate in psid varchar 200 in certificateid int 32 in number varchar 50 in userid int 32 in starttime date...

儲存過程 MySQL儲存過程的使用總結

案例所涉及到的表如下 create table t goods g id varchar 20 not null comment 商品編號 g name varchar 30 default null comment 商品名稱 g price float default null comment 商...

mysql 中使用儲存過程

create definer root localhost procedure curdemo begin declare done int default 0 declare userid,repid int declare cur1 cursor for select u.userid,u.re...