Directly add content
First, the CASE conditions in the Stored Procedure
- CREATE PROCEDURE p_case(IN parameter1 INT)
- BEGIN
- DECLARE variable1 INT;
- SET variable1 = parameter1 + 1;
- CASE variable1
- WHEN 0 THEN INSERT INTO t VALUES(0);
- WHEN 1 THEN INSERT INTO t VALUES(1);
- ELSE INSERT INTO t VALUES(2);
- END CASE;
- END;
LOOPS Loop
[1] WHILE... END WHILE
[2] LOOP... END LOOP
[3] REPEAT... END REPEAT
【4】 GOTO
I. WHILE... END WHILE
- CREATE PROCEDURE p_while()
- BEGIN
- DECLARE v INT;
- SET v = 0;
- WHILE v < 5 DO
- INSERT INTO t VALUES(v);
- SET v = v + 1;
- END WHILE;
- END;//
2. LOOP... END LOOP
- CREATE PROCEDURE p_loop()
- BEGIN
- DECLARE v INT;
- SET v = 0;
- loop_label:LOOP
- INSERT INTO t VALUES(v);
- SET v = v + 1;
- IF v>=5 THEN
- LEAVE loop_label;
- END IF;
- END LOOP;
- END;//
Iii. REPEAT... END REPEAT
- CREATE PROCEDURE p_repeat()
- BEGIN
- DECLARE v INT;
- SET v = 0;
- REPEAT
- INSERT INTO t VALUES(v);
- SET v = v + 1;
- UNTIL v>=5
- END REPEAT;
- END;//
Stored Procedure:
Returns
- CREATE PROCEDURE optionsrank(
- OUT pl INT,
- OUT ph INT,
- OUT pa INT
- )
- BEGIN
- SELECT MAX(option_id) INTO ph FROM wp_options;
- SELECT Min(option_id) INTO pl FROM wp_options;
- SELECT AVG(option_id) INTO pa FROM wp_options;
- END;
- CALL optionsrank(@pl,@ph,@pa) //
- SELECT @pl,@ph,@pa //
In and out have input AND output parameters.
- CREATE PROCEDURE in_out_test(
- IN in_option_id INT,
- OUT out_option_value TEXT
- )
- BEGIN
- SELECT option_value INTO out_option_value FROM wp_options WHERE option_id = in_option_id;
- END;
- CALL in_out_test(100,@out) //
- SELECT @out //