mysql存儲過程

mysql存儲過程編寫 1 html

mysql存儲過程 2 mysql

CONCAT(person_no,"號犯人住", i , "號牀位"); ---字符串拼接sql

存儲過程的建立標準函數

DELIMITER $$  -- 定義語句結束符 爲$$
DROP PROCEDURE IF EXISTS helloworld$$  --這裏就至關與" ; "
CREATE PROCEDURE helloworld()
BEGIN 	
	SELECT "hello world";
END $$
DELIMITER ;  --還原語句結束符爲;

建立存儲過程的方法 IN OUT INOUToop

---IN  參數的使用
DELIMITER//
CREATE PROCEDURE sp_demo_in_parameter(IN p_in INT)
BEGIN
SELECT 4;
SELECT p_in;
SET p_in=2;
SELECT p_in;
END;
//


SET @p_in=1;

CALL sp_demo_in_parameter(3)

DROP PROCEDURE sp_demo_in_parameter;

--OUT 參數的使用
DELIMITER //
CREATE PROCEDURE sp_demo_out_parameter(OUT p_out INT)
BEGIN
SELECT p_out;
SET p_out=2;
SELECT p_out;
END;
//

SET @p_out=1;
CALL sp_demo_out_parameter(@p_out);

--inout 參數的使用
DELIMITER //
CREATE PROCEDURE sp_demo_inout_parameter(INOUT p_inout INT)
BEGIN
SELECT p_inout;
SET p_inout=2;
SELECT p_inout;
END;
//

SET @p_inout=1;
CALL sp_demo_inout_parameter(@p_inout);

存儲過程的定義和調用

DELIMITER $$
DROP PROCEDURE IF EXISTS helloworld $$
CREATE PROCEDURE helloworld(normal_price NUMERIC(8,2),OUT discount_price NUMERIC(8,2))
BEGIN
	IF (normal_price > 500) THEN
		SET discount_price=normal_price*0.8;
	ELSEIF (normal_price > 100) THEN
		SET discount_price = normal_price * 0.9;
	ELSE 
		SET discount_price = normal_price;
	END IF;		
 END $$

 CALL helloworld(600,@newprice);
 SELECT @newprice

while do end while 循環ui

DELIMITER $$ 
DROP PROCEDURE IF EXISTS helloworld$$
CREATE PROCEDURE helloworld(OUT counter INT)
BEGIN 
	SET counter=0;
	WHILE counter !=10 DO
		SET counter =counter +1;
	END WHILE;
END $$

CALL helloworld(@counter);
SELECT @counter;

repeat until 替換.net

DELIMITER $$
DROP PROCEDURE IF EXISTS simple_repeat$$
CREATE PROCEDURE simple_repeat(OUT counter INT)
BEGIN 
	SET counter = 0;
	REPEAT 
		SET counter = counter +1;
	UNTIL counter =10 
	END REPEAT;
END $$

CALL simple_repeat(@repeat_counter);
SELECT @repeat_counter;

遊標的使用

查詢表中最後的一行數據code

CURSOR用於處理多行記錄的查詢結果

DECLARE LOOP_T CURSOR FOR --遊標的宣言

OPEN LOOP_T;   --打開遊標

FETCH NEXT FROM LOOP_T INTO [@PART](https://my.oschina.net/u/857680); -- 將提取結果帶入遊標

CLOSE LOOP_T; -- 關閉遊標

在MySQL的存儲過程當中常常會看到這句話:DECLARE countinue handler for NOT FOUND。它的含義是:若沒有數據返回,程序繼續,並將變量IS_FOUND設爲0 ,這種狀況是出如今select XX into XXX from tablename的時候發生的。
DELIMITER $$
DROP PROCEDURE IF EXISTS cursor_example$$
CREATE PROCEDURE cursor_example(OUT row_message INT)
	READS SQL DATA
        --整個過程當中全部的SQL語句僅須要讀記錄,不對記錄進行寫的操做。 
BEGIN
	DECLARE 1_id INT;
	DECLARE 1_mapguid VARCHAR(64);
	DECLARE 1_fac_code VARCHAR(64);
	DECLARE done INT DEFAULT 0;

	DECLARE cur1 CURSOR FOR SELECT id,mapguid,fac_code FROM ht_t_2w_map;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
	
	OPEN cur1;
		map_loop:LOOP
			FETCH cur1 INTO 1_id,1_mapguid,1_fac_code;
			IF done =1 THEN			
				LEAVE map_loop;
			END IF;
		END LOOP map_loop;	
	CLOSE cur1;	
	SET row_message=1_id;
END $$

CALL cursor_example(@row_message);
SELECT @row_message;

update insert delete

DELIMITER $$
DROP PROCEDURE IF EXISTS sp_update_map$$
CREATE PROCEDURE sp_update_map(in_id INT,in_mapName VARCHAR(64))
BEGIN
	UPDATE ht_t_2w_map SET mapName=in_mapName 
		WHERE id=in_id;
END $$

CALL sp_update_map(173,"施爺");
DELIMITER $$
DROP PROCEDURE IF EXISTS sp_insert_map$$
CREATE PROCEDURE sp_insert_map(in_mapguid VARCHAR(64), in_fac_code VARCHAR(64), in_mapName VARCHAR(64))
BEGIN 
	INSERT INTO ht_t_2w_map (mapGuid,fac_code,mapName) VALUES (in_mapguid,in_fac_code,in_mapName);
END $$

CALL sp_insert_map(UPPER(REPLACE(UUID(),'-','')),"0001","張三");

SELECT UPPER(REPLACE(UUID(),'-',''));


 在mysql中,可使用uuid 來生成主鍵,可是用mysql的uuid()函數 ,生成的uuid是36位的,
 其中包含32個字符以及4個分隔符(-),每每這個分隔符對咱們來講是沒有用的,
 可使用mysql自帶的replace函數去掉分隔符
REPLACE(UUID(),'-','')   ---->將uuid()中的‘-’,去掉,即替換成空串;
此外upper(REPLACE(UUID(),'-',''))用於將字符轉換爲大寫

NO SQL:表示子程序不包含SQL語句。orm

READS SQL DATA:表示子程序包含讀數據的語句,但不包含寫數據的語句。htm

MODIFIES SQL DATA:表示子程序包含寫數據的語句。

SQL SECURITY DEFINER:表示執行存儲過程當中的程序是由建立該存儲過程的用戶的權限來執行。

SQL SECURITY INVOKER:表示執行存儲過程當中的程序是由調用該存儲過程的用戶的權限來執行。

存儲方法

存儲方法與存儲過程的區別

1,存儲方法的參數列表只容許IN類型的參數,並且不必也不容許指定IN關鍵字

2,存儲方法返回一個單一的值,值的類型在存儲方法的頭部定義

3,存儲方法能夠在SQL語句內部調用

4,存儲方法不能返回結果集

DELIMITER $$
DROP FUNCTION IF EXISTS f_discount_price$$
CREATE FUNCTION  f_discount_price
	(normal_price NUMERIC(8,2))
	RETURNS NUMERIC(8,2)
	DETERMINISTIC
BEGIN
	DECLARE discount_price NUMERIC(8,2);  
	
	IF (normal_price>500) THEN
		SET discount_price=normal_price*0.8;
	ELSEIF (normal_price>100) THEN
		SET discount_price=normal_price*0.9;
	ELSE 
		SET discount_price=normal_price;
	END IF;
	
	RETURN  (discount_price);
END$$

一個複雜的例子-----------------------------------------------

/*給定一個id 讓該讓其查ag_prisoner表 中看是否存在該犯人*/
DELIMITER $$
DROP FUNCTION IF EXISTS  personNo$$
CREATE FUNCTION personNo(person_no BIGINT)              
RETURNS BIGINT                                                          
BEGIN  
	DECLARE person_exit BIGINT;
	SET person_exit=person_no;	
	WHILE (SELECT pr_no  FROM ag_prisoner WHERE pr_no=person_exit) IS NULL   DO
		SET person_exit = person_exit + 1;
	END WHILE;
	RETURN (person_exit);
END$$

/*如今ht_t_2w_map表中生成一條數據  
  再在ag_prisoner表中查存在的編號
  再在ag_prisoner_bed表中插入一些數據
   personNo(person_no); 他會返回一個存在的person的編號
 */
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_map_bed$$
CREATE PROCEDURE insert_map_bed(IN person_no BIGINT)
BEGIN 
	DECLARE i INT; /*牀位的循環變量*/
	DECLARE n INT;/*監舍的循環變量*/
	DECLARE uuid_val VARCHAR(32); 			/*牀位的uuid值*/
	DECLARE uuid_jianshe VARCHAR(32);		/*監舍的uuid的值*/
	DECLARE person_code VARCHAR(32);		/*牀位的code值*/
	DECLARE person_code_jianshe VARCHAR(32);	/*監舍的code值*/
	DECLARE person_code_default VARCHAR(32); 	/*默認的code的值*/
	DECLARE person_bed VARCHAR(32);			/*牀位的信息*/
	DECLARE person_name VARCHAR(32);            	/*牀位的名字*/
	DECLARE person_name_jianshe VARCHAR(32);    /*監舍的名字*/
	
	SET person_code_default="00000001";
	
	SET n=3;
	WHILE n!=5 DO
		
		/*插入監舍*/
		SET uuid_jianshe=UPPER(REPLACE(UUID(),'-',''));
		SET person_code_jianshe=CONCAT(person_code_default, "000",  n);
		SET person_name_jianshe=CONCAT("10", n,"監舍");
		INSERT INTO ht_t_2w_map(mapguid, fac_code, mapname, maplevel, mapType)  VALUES  (uuid_jianshe, person_code_jianshe,person_name_jianshe,"3","4");
	
		/*插入牀位*/
		SET i=1;
		WHILE i !=3 DO
			/*他會返回一個存在的person的編號*/
			SET person_no=personNo(person_no);
			
			SET uuid_val=UPPER(REPLACE(UUID(),'-','')); /*設置牀鋪的uuid值*/	
			SET person_code=CONCAT(person_code_jianshe,"000",i); /*牀鋪的編號*/
			SET person_bed=CONCAT(person_no,"號犯人住", i , "號牀位"); /*牀鋪上住的犯人的名稱*/
			SET person_name=CONCAT(i,"號牀位");   /*牀鋪的名稱*/
			/*先插入ht_t_2w_map表中*/
			INSERT INTO ht_t_2w_map(mapguid,fac_code,mapname,maplevel,mapType,parentGuid)  VALUES  (uuid_val, person_code,person_name,"4","5",uuid_jianshe);
			/*在插入ag_prisoner_bed表中*/
			INSERT INTO ag_prisoner_bed(pb_pr_no,pb_bed_code,pb_bed_name) VALUES (person_no,person_code,person_bed);
			
			SET person_no=person_no+1;
			SET i=i+1;
		END WHILE;
		
		SET n=n+1;
	END WHILE;
END $$

CALL insert_map_bed(6501003731);

5 根據首字母查詢信息

DELIMITER $$
DROP FUNCTION IF EXISTS GET_FIRST_PINYIN_CHAR$$
CREATE FUNCTION GET_FIRST_PINYIN_CHAR(PARAM VARCHAR(255)) RETURNS VARCHAR(255) DETERMINISTIC
BEGIN 
	DECLARE V_RETURN VARCHAR(255) ;
	DECLARE V_FIRST_CHAR VARCHAR(2) ;  
	SET V_FIRST_CHAR = UPPER(LEFT(PARAM,1));
	SET V_RETURN = V_FIRST_CHAR;
	IF LENGTH( V_FIRST_CHAR) <> CHARACTER_LENGTH( V_FIRST_CHAR ) 
		THEN  
			SET V_RETURN = ELT(INTERVAL(CONV(HEX(LEFT(CONVERT(PARAM USING gbk),1)),16,10),  
			0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,  
			0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,  
			0xC8F6,0xCBFA,0xCDDA,0xCEF4,0xD1B9,0xD4D1),  
			'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z');  
	END IF;  
	RETURN V_RETURN;  
END$$

SELECT de_name,GET_FIRST_PINYIN_CHAR(de_name) FROM jfp_dept WHERE get_first_pinyin_char(de_name) = 's'
相關文章
相關標籤/搜索