procedure/function/trigger

-- , ; 
-- delimiter ; ; 
delimiter $$
-- rand_string
drop function rand_string $$
-- rand_string(n): n 
create function rand_string(n INT)
returns varchar(255)
begin
 declare chars_str varchar(100)default 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 declare return_str varchar(255) default '';
 declare i int default 0;
 while i < n do
  --concat('a','b'):ab
  --substring(str,pos,len): str pos len 
  --rand(): [0,1] 
  set return_str = concat(return_str,substring(chars_str,floor(1 + rand() * 52),1));
  set i = i + 1;
 end while;
 return return_str;
end $$

delimiter ;
-- $$
delimiter $$
-- rand_num(): 
create function rand_num()
returns int(5)
begin
 declare i int defaule 0;
 set i = floor(10 + rand() * 50);
 return i;
end $$
-- ;
delimiter ;

-- $$
delimiter $$
-- 
create procedure insert_emp(in start int(10), in max_num int(10))
begin
 declare i int default 0;
 set autocommit = 0;
 repeat
  set i = i + 1;
  insert into emp values(start + i, rand_string(6), 'SALESMAN'.0001,curdate(), 2000);
  until i = max_num
 end repeat;
 commit;
end $$
-- ;
delimiter ;
-- 
call inert_emp(100001, 4800000);

저장 프로세스 예, 2015-5-22 23:20, dzg 프로젝트 기반
delimiter $$
DROP PROCEDURE
IF EXISTS update_purchase_pro_refundable$$

CREATE PROCEDURE update_purchase_pro_refundable (
	IN productId VARCHAR (50),
	IN supplierId VARCHAR (50),
	IN shopId VARCHAR(50),
	IN counts INT(10)
)
BEGIN
	DECLARE purchaseProId VARCHAR(50);
	DECLARE error_status INT DEFAULT 0 ; 
	DECLARE datas CURSOR FOR (
				SELECT
					pp.id
				FROM
					purchase_pro pp
				LEFT JOIN purchase pur ON pp.purchase_id = pur.id
				WHERE
					pur.shop_id = shopId
				AND pp.product_id = productId
				AND pp.supplier_id = supplierId
				ORDER BY
					pur.time DESC
				LIMIT counts
			);
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET error_status=1;
	UPDATE purchase_pro pp LEFT JOIN purchase pur ON pp.purchase_id=pur.id
	SET refundable = '0'
	WHERE
		product_id = productId
	AND supplier_id = supplierId
	AND pur.shop_id=shopId;

	OPEN datas;
		FETCH datas INTO purchaseProId;

		REPEAT
		UPDATE purchase_pro SET refundable='1' WHERE id=purchaseProId;
		FETCH datas INTO purchaseProId;
		UNTIL  error_status
		END REPEAT;

	CLOSE  datas;
END
$$
delimiter ;

 
모든 기능 보기
show function status

호출 함수
select  ()

스토리지 프로세스의 정의 보기
show create function  

모든 스토리지 프로세스 보기
show procedure status
어떤 저장 프로세스의 정의를 보기
show create procedure  

저장 프로세스 호출
call  ()

모든 이벤트 보기
show events

모든 트리거 보기
show triggers

좋은 웹페이지 즐겨찾기