崇之他和她

导航

mysql 存储过程

--alter table shiyanshuju.gdpoint add S_X_S1 double(10,3),add S_Y_S1 double(10,3),add E_X_S1 double(10,3),add E_Y_S1 double(10,3),add S_H_S1 float(9,3),add E_H_S1 float(9,3),add SE_S1 VARCHAR(255),
add D_S_S1 VARCHAR(20),add E_Deep_S1 double(10,3),add S_Deep_S1 double(10,3),add D_Type_S1 VARCHAR(20);--

存储过程调用 方法

call delColoums (1,2)
--参数 字段排序开始,字段排序结束
call addColoums(1,1)

**** 存储过程 1:循环增加字段****

DROP PROCEDURE IF EXISTS `hd_ybz`.`addColoums`;

CREATE DEFINER=`root`@`localhost` PROCEDURE `addColoums`(IN `startnum` int,IN `endnum` int)
BEGIN
 DECLARE num int default 1; 
 WHILE startnum <= endnum DO

		 set @addSql = CONCAT('alter table shiyanshuju.gdpoint add S_X_S',startnum,' double(10,3), add S_Y_S',startnum,'  double(10,3),add E_X_S',startnum,'  double(10,3),add E_Y_S',startnum,'  double(10,3),add S_H_S',startnum,'  float(9,3),add E_H_S',startnum,'  float(9,3),add SE_S',startnum,'  VARCHAR(255),
		add D_S_S',startnum,' VARCHAR(20),add E_Deep_S',startnum,' double(10,3),add S_Deep_S',startnum,' double(10,3),add D_Type_S',startnum,' VARCHAR(20)');
							
				prepare addstmt from @addSql;
			 
				execute addstmt;
		 
				DEALLOCATE PREPARE addstmt;
							 
				set startnum = (startnum+num);
	
	END WHILE;
			IF startnum > endnum THEN
				set startnum = endnum;
			END IF ; 

 SELECT  CONCAT('增加字段PROCEDURE执行完毕:',startnum) result;

END;

**** 存储过程 2: 循环删除字段***

DROP PROCEDURE IF EXISTS `hd_ybz`.`delColoums`;

CREATE DEFINER=`root`@`localhost` PROCEDURE `delColoums`(IN `startnum` int,IN `endnum` int)
BEGIN      
   DECLARE num int default 1; 
		WHILE startnum <= endnum DO
		 

		 set @delSql = CONCAT('alter table shiyanshuju.gdpoint drop column S_X_S',startnum,' ,drop column S_Y_S',startnum,'  ,drop column E_X_S',startnum,'  ,drop column E_Y_S',startnum,'  ,drop column S_H_S',startnum,'  ,drop column E_H_S',startnum,'  ,drop column SE_S',startnum,',

drop column D_S_S',startnum,',drop column E_Deep_S',startnum,',drop column S_Deep_S',startnum,',drop column D_Type_S',startnum,'');
							

				prepare delstmt from @delSql;
			 
				execute delstmt;
		 
				DEALLOCATE PREPARE delstmt;
				
				set startnum = (startnum+num);
	END WHILE;
			IF startnum > endnum THEN
				set startnum = endnum;
			END IF ; 

 SELECT  CONCAT('删除字段PROCEDURE执行完毕:',startnum) result;
	

END;

posted on 2022-04-22 11:22  崇之他和她  阅读(10)  评论(0编辑  收藏  举报