-
[MySQL] Stored FunctionDB/MySQL 2024. 1. 21. 22:20반응형
Stored Function
- A set of SQL statements that perform a specific task and return a single value
- Allow to encapsulate a series of SQL statements into a reusable and modular unit
DELIMITER // -- change DELIMITER ";" to "//" temporarily CREATE FUNCTION calculate_area(length DOUBLE, width DOUBLE) RETURNS DOUBLE BEGIN DECLARE area DOUBLE; SET area = length * width; RETURN area; END // DELIMITER ; -- back to original delimiter
DELIMITER
- Statement used to change the statement terminator temporarily
- Helps defining stored functions or procedures without interference from the default statement terminator(;) used by the MySQL command-line clientCREATE FUNCTION
- Statement used to define a new stored function
- function_name : name of function that you are creating (calculate_area is function name in above example)- parameters : Variables that can use within the function and each parameter has a name and a data type
RETURNS return_datatype
- The data type of the value that the function will return
- If function doesnt' return a value, use 'RETURNS VOID' or omit the 'RETURNS' clause
BEGIN...END
- The body of the function
- SQL statements that perform the desired task between these keywords
RETURN expression
- Statement used to return a value from the function
DECLARE
- Statement used to declare variables within a stored function
- Variables can be used to store and manipulate data within the scope of the stored function- The '@' symbol is used to denote declared variable
반응형'DB > MySQL' 카테고리의 다른 글
[MySQL] Stored Procedure (0) 2024.01.22 [MySQL/Java] DB 쿼리 성능개선 (2) 랭킹 조회 속도 개선하기 (5s ➡️ 1s대로 줄이기!) (0) 2023.06.29 [MySQL/Java] DB 쿼리 성능개선 (1) test용 mock/bulk data 삽입 방법 (0) 2023.06.26