mysql存儲過程中使用變量批量修改數據
BEGIN
declare i int;#查找表的列數
declare column_name_temp varchar(100);##查找表的列名
DECLARE column_un_change varchar(100);
DECLARE table_temp_name varchar(100);
declare description varchar(100);
declare code varchar(100);
set column_un_change = 'OTHER_INCOME';
set description='description';
set code = 'code'; www.2cto.com
set table_temp_name = 'wy_notebill_detail2';
######################查找表的列名存儲到臨時表###################
drop table if EXISTS update_column;
create temporary table update_column (a INT NOT NULL AUTO_INCREMENT,PRIMARY KEY (a))
TYPE = MyISAM
SELECT column_name from information_schema.`COLUMNS` where table_name = table_temp_name and column_name not in(column_un_change,description,code) and data_type not in ('date','datetime');
#######################統計列數量####################
SELECT count(*) into i from update_column;
SELECT i; www.2cto.com
loop1: WHILE i>2 DO
##########################給變量賦值列名#######################
SELECT column_name into column_name_temp from update_column where a = i;
SET i=i-1;
#########################更新表的數據##########################
set @sqls=concat('update ',table_temp_name ,' set ',column_name_temp,' = 0');
PREPARE stmt1 from @sqls;
EXECUTE stmt1 ;
END WHILE loop1;
#SELECT * from wy_notebill_detail;
END
作者 劉宗才