存储过程之代码块、条件控制、迭代
2009-03-31 16:52
302 查看
1,代码块
代码顺序
1,变量和条件声明
2,Cursor声明
3,Handler声明
4,程序代码
可以给代码块加lebel,这样END匹配比较直观,还可以用LEAVE语句来终结代码块:
[label:] BEGIN
varaiable and condition declarations
cursor declarations
handler declarations
program code
END [label];
代码块可以嵌套:
CREATE PROCEDURE nested_blocks()
BEGIN
DECLARE my_variable varchar(20);
SET my_variable='This value was set in the outer block';
BEGIN
SET my_variable='This value was set in the inner block';
END;
SELECT my_variable, 'Changes in the inner block are visible in the outer block';
END;
LEAVE的例子:
CREATE PROCEDURE nested_blocks()
outer_block: BEGIN
DECLARE l_status int;
SET l_status=1;
inner_block: BEGIN
IF (l_status=1) THEN
LEAVE inner_block;
END IF;
SELECT 'This statement will never be executed';
END inner_block;
SELECT 'End of program';
END outer_block;
2,条件控制
IF:
IF expression THEN commands
[ELSEIF expression THEN commands]
[ELSE commands]
END IF;
例子:
IF (sale_value > 200) THEN
CALL free_shipping(sale_id); /*Free shipping*/
IF (customer_status='PLATINUM') THEN
CALL apply_discount(sale_id,20); /* 20% discount */
ELSEIF (customer_status='GOLD') THEN
CALL apply_discount(sale_id,15); /* 15% discount */
ELSEIF (customer_status='SILVER') THEN
CALL apply_discount(sale_id,10); /* 10% discount */
ELSEIF (customer_status='BRONZE') THEN
CALL apply_discount(sale_id,5); /* 5% discount*/
END IF;
END IF;
CASE:
CASE
WHEN condition THEN
statements
[WHEN condition THEN
statements...]
[ELSE
statements]
END CASE;
例子:
CASE
WHEN (sale_value>200) THEN
CALL free_shipping(sale_id);
CASE customer_status
WHEN 'PLATINUM' THEN
CALL apply_discount(sale_id,20);
WHEN 'GOLD' THEN
CALL apply_discount(sale_id,15);
WHEN 'SILVER' THEN
CALL apply_discount(sale_id,10);
WHEN 'BRONZE' THEN
CALL apply_discount(sale_id,5);
END CASE;
END CASE;
CASE与SELECT语句结合的妙用:
SELECT (CASE WHEN (t.a = 1 AND t.b = 0) THEN t.c ELSE 'N/A' END) AS result FROM test t order by result asc
3,迭代
LOOP
[label:] LOOP
statements
END LOOP [label];
REPEAT...UNTIL
[label:] REPEAT
statements
UNTIL expression
END REPEAT [label]
WHILE
[label:] WHILE expression DO
statements
END WHILE [label]
LEAVE语句
SET i=1;
myloop: LOOP
SET i=i+1;
IF i=10 then
LEAVE myloop;
END IF:
END LOOP myloop;
SELECT 'I can count to 10';
ITERATE语句
SET i=0;
loop1: LOOP
SET i=i+1;
IF i>=10 THEN /*Last number - exit loop*/
LEAVE loop1;
ELSEIF MOD(i, 2)=0 THEN /*Even number - try again*/
ITERATE loop1;
END IF;
SELECT CONCAT(i, " is an odd number");
END LOOP loop1;
嵌套循环
DECLARE i, j INT DEFAULT 1;
outer_loop: LOOP
SET j=1;
inner_loop: LOOP
SELECT concat(i, " times ", j, " is ", i*j);
SET j=j+1;
IF j>12 THEN
LEAVE inner_loop;
END IF;
END LOOP inner_loop;
SET i=i+1;
IF i>12 THEN
LEAVE outer_loop;
END IF;
END LOOP outer_loop;
代码顺序
1,变量和条件声明
2,Cursor声明
3,Handler声明
4,程序代码
1,变量和条件声明 2,Cursor声明 3,Handler声明 4,程序代码
可以给代码块加lebel,这样END匹配比较直观,还可以用LEAVE语句来终结代码块:
[label:] BEGIN
varaiable and condition declarations
cursor declarations
handler declarations
program code
END [label];
[label:] BEGIN varaiable and condition declarations cursor declarations handler declarations program code END [label];
代码块可以嵌套:
CREATE PROCEDURE nested_blocks()
BEGIN
DECLARE my_variable varchar(20);
SET my_variable='This value was set in the outer block';
BEGIN
SET my_variable='This value was set in the inner block';
END;
SELECT my_variable, 'Changes in the inner block are visible in the outer block';
END;
CREATE PROCEDURE nested_blocks() BEGIN DECLARE my_variable varchar(20); SET my_variable='This value was set in the outer block'; BEGIN SET my_variable='This value was set in the inner block'; END; SELECT my_variable, 'Changes in the inner block are visible in the outer block'; END;
LEAVE的例子:
CREATE PROCEDURE nested_blocks()
outer_block: BEGIN
DECLARE l_status int;
SET l_status=1;
inner_block: BEGIN
IF (l_status=1) THEN
LEAVE inner_block;
END IF;
SELECT 'This statement will never be executed';
END inner_block;
SELECT 'End of program';
END outer_block;
CREATE PROCEDURE nested_blocks() outer_block: BEGIN DECLARE l_status int; SET l_status=1; inner_block: BEGIN IF (l_status=1) THEN LEAVE inner_block; END IF; SELECT 'This statement will never be executed'; END inner_block; SELECT 'End of program'; END outer_block;
2,条件控制
IF:
IF expression THEN commands
[ELSEIF expression THEN commands]
[ELSE commands]
END IF;
IF expression THEN commands [ELSEIF expression THEN commands] [ELSE commands] END IF;
例子:
IF (sale_value > 200) THEN
CALL free_shipping(sale_id); /*Free shipping*/
IF (customer_status='PLATINUM') THEN
CALL apply_discount(sale_id,20); /* 20% discount */
ELSEIF (customer_status='GOLD') THEN
CALL apply_discount(sale_id,15); /* 15% discount */
ELSEIF (customer_status='SILVER') THEN
CALL apply_discount(sale_id,10); /* 10% discount */
ELSEIF (customer_status='BRONZE') THEN
CALL apply_discount(sale_id,5); /* 5% discount*/
END IF;
END IF;
IF (sale_value > 200) THEN CALL free_shipping(sale_id); /*Free shipping*/ IF (customer_status='PLATINUM') THEN CALL apply_discount(sale_id,20); /* 20% discount */ ELSEIF (customer_status='GOLD') THEN CALL apply_discount(sale_id,15); /* 15% discount */ ELSEIF (customer_status='SILVER') THEN CALL apply_discount(sale_id,10); /* 10% discount */ ELSEIF (customer_status='BRONZE') THEN CALL apply_discount(sale_id,5); /* 5% discount*/ END IF; END IF;
CASE:
CASE
WHEN condition THEN
statements
[WHEN condition THEN
statements...]
[ELSE
statements]
END CASE;
CASE WHEN condition THEN statements [WHEN condition THEN statements...] [ELSE statements] END CASE;
例子:
CASE
WHEN (sale_value>200) THEN
CALL free_shipping(sale_id);
CASE customer_status
WHEN 'PLATINUM' THEN
CALL apply_discount(sale_id,20);
WHEN 'GOLD' THEN
CALL apply_discount(sale_id,15);
WHEN 'SILVER' THEN
CALL apply_discount(sale_id,10);
WHEN 'BRONZE' THEN
CALL apply_discount(sale_id,5);
END CASE;
END CASE;
CASE WHEN (sale_value>200) THEN CALL free_shipping(sale_id); CASE customer_status WHEN 'PLATINUM' THEN CALL apply_discount(sale_id,20); WHEN 'GOLD' THEN CALL apply_discount(sale_id,15); WHEN 'SILVER' THEN CALL apply_discount(sale_id,10); WHEN 'BRONZE' THEN CALL apply_discount(sale_id,5); END CASE; END CASE;
CASE与SELECT语句结合的妙用:
SELECT (CASE WHEN (t.a = 1 AND t.b = 0) THEN t.c ELSE 'N/A' END) AS result FROM test t order by result asc
SELECT (CASE WHEN (t.a = 1 AND t.b = 0) THEN t.c ELSE 'N/A' END) AS result FROM test t order by result asc
3,迭代
LOOP
[label:] LOOP
statements
END LOOP [label];
[label:] LOOP statements END LOOP [label];
REPEAT...UNTIL
[label:] REPEAT
statements
UNTIL expression
END REPEAT [label]
[label:] REPEAT statements UNTIL expression END REPEAT [label]
WHILE
[label:] WHILE expression DO
statements
END WHILE [label]
[label:] WHILE expression DO statements END WHILE [label]
LEAVE语句
SET i=1;
myloop: LOOP
SET i=i+1;
IF i=10 then
LEAVE myloop;
END IF:
END LOOP myloop;
SELECT 'I can count to 10';
SET i=1; myloop: LOOP SET i=i+1; IF i=10 then LEAVE myloop; END IF: END LOOP myloop; SELECT 'I can count to 10';
ITERATE语句
SET i=0;
loop1: LOOP
SET i=i+1;
IF i>=10 THEN /*Last number - exit loop*/
LEAVE loop1;
ELSEIF MOD(i, 2)=0 THEN /*Even number - try again*/
ITERATE loop1;
END IF;
SELECT CONCAT(i, " is an odd number");
END LOOP loop1;
SET i=0; loop1: LOOP SET i=i+1; IF i>=10 THEN /*Last number - exit loop*/ LEAVE loop1; ELSEIF MOD(i, 2)=0 THEN /*Even number - try again*/ ITERATE loop1; END IF; SELECT CONCAT(i, " is an odd number"); END LOOP loop1;
嵌套循环
DECLARE i, j INT DEFAULT 1;
outer_loop: LOOP
SET j=1;
inner_loop: LOOP
SELECT concat(i, " times ", j, " is ", i*j);
SET j=j+1;
IF j>12 THEN
LEAVE inner_loop;
END IF;
END LOOP inner_loop;
SET i=i+1;
IF i>12 THEN
LEAVE outer_loop;
END IF;
END LOOP outer_loop;
相关文章推荐
- MySQL存储过程之代码块、条件控制、迭代
- MySql存储过程—5、逻辑判断,条件控制
- MySql存储过程—5、逻辑判断,条件控制
- MySql存储过程之逻辑判断和条件控制
- MySql存储过程—逻辑判断,条件控制,循环控制
- MySql存储过程—5、逻辑判断,条件控制
- MySql存储过程之逻辑判断和条件控制
- MySql存储过程—逻辑判断,条件控制,循环控制
- SQL Server不能通过条件控制生成同名的存储过程
- 过滤注入代码的存储过程
- Java代码调用存储过程和存储方法
- aspnetpager 分页存储过程的实现 全代码
- SQL Server 存储过程中处理多个查询条件的几种常见写法分析,我们该用那种写法
- SQL Server 分页查询存储过程代码
- 存储过程分页,按多条件排序(原创)
- oracle 存储过程代码
- oracle查看所有函数或存储过程的代码
- C++ 用条件编译 控制开发过程中输出调试信息
- SQL Server 第四堂课,创建存储过程。存储过程是一组编译在单个执行计划中的transact-SQL语句。存储过程相当于C#函数,可以允许模块化程序设计,允许更快执行如果某操作需要大量transct-SQL代码或需要重复执行,将在创建存储过程中对其进行分析和优化。