>데이터 베이스 >MySQL 튜토리얼 >mysql存储过程读书笔记二(control and conditional logic)

mysql存储过程读书笔记二(control and conditional logic)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB원래의
2016-06-07 16:16:121158검색

mysql存储过程读书笔记2(control and conditional logic) 1. If create procedure example3(stu_id int) begin ??? declare stu_name varchar(30); ? ???? select name into stu_name from student where student_id = stu_id; ???? If stu_name is not null

mysql存储过程读书笔记2(control and conditional logic)

1. If

create procedure example3(stu_id int)
begin
??? declare stu_name varchar(30);
?
???? select name into stu_name from student where student_id = stu_id;
???? If stu_name is not null Then
??????? ?select stu_name;
???? End If;
End;

?

2. If ... Else....

create procedure example4(stu_id int)
begin
???? declare stu_name varchar(30);
?
????? select name into stu_name from student where student_id = stu_id;
???? ?If stu_name is not null Then
??????????????? select stu_name;
???????Else
??????????????? ?select 'student is not exist';
???????End If;
End;

?

3. IF ... ELSEIF ... ELSE ... END;

drop procedure if exists discounted_price;
create procedure discounted_price(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 > 400) Then
??????? SET discount_price = normal_price * 0.9;
????Else
???????? SET discount_price = normal_price;
???? End If;
End

?

4. Loop

?

drop procedure if exists simple_loop;
create procedure simple_loop()
begin
?DECLARE counter int;
?SET counter = 0;
?
?my_simple_loop: LOOP
??IF counter = 10 Then
???LEAVE my_simple_loop;
??END IF;
??
??SET counter = counter + 1;
?END LOOP my_simple_loop;
?
?select ' now counter is 10';
END

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.