Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

MYSQL PL/SQL

Shulou Source: shulou.com Published: 2022-06-03 04:17:39 09月14日 Update

A set of SQL statements with high speed. Only the first execution needs to go through compilation and optimization steps, and the subsequent calls can be executed directly.

Sample:

DROP PROCEDURE IF EXISTS `proc_ adder`

DELIMITER & &

CREATE DEFINER= `root` @ `localhost` PROCEDURE `proc_ adder` (IN an int, IN b int, OUT sum int)

BEGIN

DECLARE c int

/ * DECLARE c int DEFAULT 10 minutes /

If an is null then set a = 0

End if

If b is null then set b = 0

End if

Set sum = a + b

END

& &

DELIMITER

Description:

DEFINER: creator

DECLARE: defining variabl

SET: assign to a variable

SELECT... The INTO statement assigns values to variables as follows:

SELECT col_name [,...] INTO var_name [,...]

FROM table_name WEHRE condition

DELIMITER:

The default statement Terminator in MySQL is the semicolon (;). The SQL statement in the stored procedure requires a semicolon (;) to end. To avoid conflicts, first set the MySQL Terminator to & & with "DELIMITER & &". Finally, use "DELIMITER;" to restore the Terminator to a semicolon. This is the same as when you create a trigger.

Common syntax:

IF:

IF type = 0 THEN

Set c = 'param is 0'

ELSEIF type = 1 THEN

Set c = 'param is 1'

ELSE

Set c = 'param is others, not 0 or 1'

END IF

CASE:

CASE type

WHEN 0 THEN

Set c = 'param is 0'

WHEN 1 THEN

Set c = 'param is 1'

ELSE

Set c = 'param is others, not 0 or 1'

END CASE

WHILE:

DECLARE i int

DECLARE s int

SET I = 0

SET s = 0

WHILE i

Tags: Statements semicolons variables Terminators creators only commonly used steps triggers syntax procedures speed speed conflicts storage compilation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL OPPO Reno NVidia MariaDB