当前位置:网站首页>Introduction to stored procedure testing

Introduction to stored procedure testing

2022-06-26 09:54:00 Miming

CREATE PROCEDURE `test`.`new_procedure` ()
BEGIN
--  Need to define variables to receive cursor data  
  DECLARE a CHAR(16);
  --  The cursor 
  DECLARE cur CURSOR FOR SELECT i FROM test.t;
  --  Traversal data end flag 
  DECLARE done INT DEFAULT FALSE;
  --  Bind the end flag to the cursor 
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  --  Open cursor 
  OPEN cur;
  
  --  Start the cycle 
  read_loop: LOOP
    --  Extract the data in the cursor , There's only one , It's the same with more than one ;
    FETCH cur INTO a;--  Grab FETCH When , If cur by null(SELECT i FROM test.t), It triggers SET done = TRUE
    --  At the end of the statement 
    IF done THEN
      LEAVE read_loop;
    END IF;
    --  Here's what you want to do with the cyclic events 

    INSERT INTO test.t VALUES (a);

  END LOOP;
  --  Close cursor 
  CLOSE cur;

END

stay MySQL We often see this sentence in the stored procedure of :DECLARE CONTINUE HANDLER FOR NOT FOUND.

It means : If no data is returned , The program continues , And change the IS_FOUND Set to 0 , This happens when select XX into XXX from tablename It happened when .

-- start by lxm
DROP PROCEDURE  IF EXISTS loan_contract_no_rule_init;
CREATE PROCEDURE loan_contract_no_rule_init()

	BEGIN
		DECLARE done INT DEFAULT 0;
		DECLARE ruleId VARCHAR(32) DEFAULT '1528655117129572354';
		DECLARE rule_detail_id_insert VARCHAR(32);
		DECLARE rule_vale_insert VARCHAR(512);
		--  Define cursors 
		DECLARE cur CURSOR FOR
			SELECT rule_detail_id as rule_detail_id_insert,rule_vale as rule_vale_insert FROM trade_rule_detail_h WHERE rule_key = 'ATTACHMENT_TYPE' AND rule_detail_id IN (SELECT rule_detail_id FROM ent_trade_rule_info t WHERE
			1 = 1 AND trade_code = 'REGISTER' AND STATUS = 1 AND prod_id = 'C01P00001' AND institution_id IN ( SELECT ent_id FROM prod_cif.cif_ent_type_ext WHERE ent_type = 9 AND prod_type = '01' ));
		DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
		--  Open cursor 
		OPEN cur;

		--  Extract the data in the cursor ;
		FETCH cur INTO rule_detail_id_insert,rule_vale_insert;

		WHILE done<>1 DO
			--  Historical snapshot insert details 
			IF (rule_vale_insert = 'SELL_PROTOCOL') THEN
		    --  Financing agreement  > initialization >  Financing Contract No 
			  INSERT INTO trade_rule_detail_h(id,rule_detail_id,rule_id,rule_key,rule_status,rule_vale)VALUES
			  (REPLACE(UUID(),'-',''),rule_detail_id_insert,ruleId,'LOAN_CONTRACT_NO',1,'LOAN_CONTRACT_NO');
			ELSEIF (rule_vale_insert = 'LAST_TRADE_BACK') THEN 
			  --  Last hand trade background materials  >  Initialize to  >  Trade Contract No 
			  INSERT INTO trade_rule_detail_h(id,rule_detail_id,rule_id,rule_key,rule_status,rule_vale)VALUES
			  (REPLACE(UUID(),'-',''),rule_detail_id_insert,ruleId,'LOAN_CONTRACT_NO',1,'BACKGROUND_CONTRACT_NO');
			ELSEIF (rule_vale_insert ='SELL_PROTOCOL,LAST_TRADE_BACK') THEN	
				--  Select the financing agreement and the last-hand trade background materials  >  initialization  >  Financing Contract No 
				INSERT INTO trade_rule_detail_h(id,rule_detail_id,rule_id,rule_key,rule_status,rule_vale)VALUES
			  (REPLACE(UUID(),'-',''),rule_detail_id_insert,ruleId,'LOAN_CONTRACT_NO',1,'LOAN_CONTRACT_NO');
			ELSE 
			  --  Nothing else 
				INSERT INTO trade_rule_detail_h(id,rule_detail_id,rule_id,rule_key,rule_status,rule_vale)VALUES
			  (REPLACE(UUID(),'-',''),rule_detail_id_insert,ruleId,'LOAN_CONTRACT_NO',1,'');
		END IF;
			FETCH cur INTO rule_detail_id_insert,rule_vale_insert;
		END WHILE;
		CLOSE cur;
	END;

CALL loan_contract_no_rule_init();

-- end by lxm
原网站

版权声明
本文为[Miming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260916480649.html