CREATE PROCEDURE Feemonth (inchFmark varchar ( $),inchFuserChar( +),inchFtime BIGINT,inchFmonthChar(6) BEGIN #定义SQL变量 declare create_sql varchar ( -); DECLARE sel_sql varchar ( -); DECLARE del_sql varchar ( -); DECLARE Fmon varchar ( -); #定义表名变量 declare tableName varchar ( -); #判断是否全部遍历记录的标记 DECLARE iint default 0; #定义游标 name is month_cursor declare month_cursor cursor for Selecttable_name fromData_import_config; #当游标遍历完成后将标记变成某个值 Declare CONTINUE HANDLER forNot foundSetI=1; SetCreate_sql='CREATE TABLE'; SetSel_sql='As select * from'; SetDel_sql='Delete from'; SetFmon=concat ('_', Fmonth); #打开游标 Open month_cursor; REPEAT #取出每条记录付给相关变量 FETCH month_cursor into TableName; Set@c_sql =CONCAT (create_sql,tablename,fmon,sel_sql,tablename); Set@d_sql =CONCAT (del_sql,tablename); PREPARE Pre_c_sql from@c_sql; PREPARE Pre_d_sql from@d_sql; EXECUTE Pre_c_sql; EXECUTE Pre_d_sql; deallocate prepare pre_c_sql; deallocate prepare pre_d_sql; #关闭游标 until I end REPEAT; Close month_cursor; Update Fee_monthSetmark=fmark,user_id=fuser,time=ftime,flag='1' whereMonth=Fmonth; Insert into Fee_month (Month,flag) VALUES (SUBSTR (Fmonth,5,2)+1) > AThen CONCAT (SUBSTR (Fmonth,1,4)+1,' on') When (SUBSTR (Fmonth,5,1)+1) <= AThen (fmonth+1) END,'0'); Insert into Fee_collect (Month,organize_id,pre_fee,user_fee,sys_fee,tmp_fee)SelectF.month,f.organize_id,f.pre_fee,f.user_fee,f.sys_fee,f.tmp_fee fromFee_collect_cur asF; Delete fromfee_collect_cur; Insert into Fee_collect_cur (Month,organize_id,pre_fee)Select(SUBSTR (Fmonth,5,2)+1) > AThen CONCAT (SUBSTR (Fmonth,1,4)+1,' on') When (SUBSTR (Fmonth,5,1)+1) <= AThen (fmonth+1) END), f.organize_id, (F.pre_fee+f.user_fee+f.tmp_fee-f.sys_fee) fromFee_collect asF WHERE (f.pre_fee+f.user_fee+f.tmp_fee-f.sys_fee)! =0; END
A statement is a reference to others.
When writing this SQL, be sure to note that:
Custom variables must not drink the names of the fields in the database!!