MySQL 저장 프로 세 스 개념,원리 와 일반적인 용법 에 대한 상세 한 설명
1.저장 과정의 개념
일부 언어 에서,예 를 들 어 pacal 은'과정'procedure 라 는 개념 과'함수'function 이 있 는데,phop 에 서 는 과정 이 없고 함수 만 있 습 니 다.
프로 세 스:몇 개의 문 구 를 봉 인 했 습 니 다.호출 할 때 이 패키지 들 은 실 행 됩 니 다.
함수:반환 값 이 있 는"과정"입 니 다.
요약:과정 은 반환 값 이 없 는 함수 입 니 다.
MySQL 에서:
우 리 는 몇 개의 sql 을 봉 하여 이름 을 짓 는 과정
이 과정 을 데이터베이스 에 저장 하 다―저장 과정
2.저장 프로시저 생 성
create procedure procedureName()
begin
//--sql
end$
3.기 존의 저장 과정 보기
show procedure status
4.저장 프로시저 삭제
drop procedure procedureName;
5.저장 프로시저 호출
call procedureName();
6.첫 번 째 저장 과정메모:MySQL 의 끝 식별 자 를$로 바 꿨 습 니 다.$로 설정 하 는 방법 을 알 고 싶다 면 다른 글 을 참고 하 십시오:MySQL 트리거.
create procedure p1()
begin
select 2+3;
end$
호출:
call p1();
결과 보이 기:7.도입 변수
저장 프로 세 스 는 프로 그래 밍 할 수 있 습 니 다.변수,표현 식,제어 구 조 를 사용 하여 복잡 한 기능 을 완성 할 수 있 음 을 의미 합 니 다.저장 과정 에서 declare 로 변 수 를 설명 합 니 다.
선언 변수 이름 변수 형식[default 기본 값]
사용:
create procedure p2()
begin
declare age int default 18;
declare height int default 180;
select concat(' :',age,' :',height);
end$
결과 보이 기:8.도입 식
저장 과정 에서 변 수 는 sql 구문 에서 합 법 적 인 연산 을 할 수 있 습 니 다.예 를 들 어+-*/.변수의 할당 형식:
set := expression
사용:
create procedure p3()
begin
declare age int default 18;
set age := age + 20;
select concat('20 :',age);
end$
결과 보이 기:9.선택 제어 구 조 를 도입 한다.
형식:
if condition then
statement
elseif
statement
else
statement
end if;
사용:
create procedure p4()
begin
declare age int default 18;
if age >= 18 then
select ' ';
else
select ' ';
end if;
end$
결과 보이 기:10.저장 프로시저 전송
저장 프로 세 스 를 정의 하 는 괄호 에서 매개 변수,문법 을 설명 할 수 있 습 니 다.
[in/out/inout]
사용:
create procedure p5(width int,height int)
begin
select concat(' :',width * height) as area;
if width > height then
select ' ';
elseif width < height then
select ' ';
else
select ' ';
end if;
end$
결과 보이 기:11.while 순환 구조 사용
필요:1 부터 100 까지 추가
사용:
create procedure p6()
begin
declare total int default 0;
declare num int default 0;
while num <= 100 do
set total := total + num;
set num := num + 1;
end while;
select total;
end$
결과 보이 기:12.저장 프로 세 스 매개 변수의 입 출력 유형
주로 in,out,inout 세 가지 유형 이 있 습 니 다.
필요:1 부터 N 까지
입력 형 데 이 터 는 우리 가 값 을 주 고 출력 형 은 우리 가 변수 이름 을 주 며 출력 을 곱 하기 위 한 변수 값 입 니 다.
(1)in 형,이때 in 은 줄 인 자 를 입력 하기 위해 우리 의 입력 을 받 아들 일 수 있 습 니 다.
create procedure p7(in n int)
begin
declare total int default 0;
declare num int default 0;
while num <= n do
set total := total + num;
set num := num + 1;
end while;
select total;
end$
호출:
call p7(100);
출력 결과:(2)out 형식의 매개 변수
create procedure p8(in n int,out total int)
begin
declare num int default 0;
set total := 0;
while num <= n do
set total := total + num;
set num := num + 1;
end while;
end$
호출:
call p8(100,@total); --100 , @total
select @total; -- @total
출력 결과:(3)inout 형식의 매개 변수
create procedure p9(inout age int)
begin
set age := age+20;
end$
호출:
set @age = 18; -- @age 18
call p9(@age); -- p9 ,@age
select @age; -- @age
출력 결과:inout 형 변수의 실제 인삼 도 변수 이름 입 니 다.이 변 수 는 저장 과정 에서 입력 변수 일 뿐만 아니 라 출력 변수 이기 도 합 니 다.
13.케이스 구조의 용법
사용:
create procedure p10()
begin
declare pos int default 0;
set pos := floor(5*rand());
case pos
when 1 then select ' ';
when 2 then select ' ';
when 3 then select ' ';
else select ' ';
end case;
end$
출력 결과:14.repeat 순환 구조
형식:
[begin_label:] REPEAT
statement_list
UNTIL search_condition
END REPEAT [end_label]
필요:1 부터 100 까지 추가
create procedure p11()
begin
declare total int default 0;
declare num int default 0;
r:repeat
set total:= total + num;
set num:=num + 1;
until num > 100
end repeat r;
select total;
end$
출력 결과:더 많은 MySQL 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 논문 에서 말 한 것 이 여러분 의 MySQL 데이터베이스 계획 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Redash를 사용할 때 몰랐던 SQL을 쓰는 법을 배웠습니다.최근 redash에서 sql을 쓸 기회가 많고, 이런 쓰는 방법이 있었는지와 sql에 대해 공부를 다시하고 있기 때문에 배운 것을 여기에 씁니다. Redash란? 월별로 데이터를 표시하고 싶습니다 주별로 데이터를 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.