Diferència entre revisions de la pàgina «Procediments emmagatzemats. Continuació»
Salta a la navegació
Salta a la cerca
(Es crea la pàgina amb «=IF. Condicionals= *https://www.mysqltutorial.org/mysql-if-statement/ =sentència CASE= *https://www.mysqltutorial.org/mysql-case-statement/ =Bulces LOOP= *https://www...».) |
|||
| Línia 1: | Línia 1: | ||
=IF. Condicionals= | =IF. Condicionals= | ||
*https://www.mysqltutorial.org/mysql-if-statement/ | *https://www.mysqltutorial.org/mysql-if-statement/ | ||
| + | Ús del IF-ELSEIF-ELSE: | ||
| + | <pre> | ||
| + | DELIMITER $$ | ||
| + | |||
| + | CREATE PROCEDURE GetCustomerLevel( | ||
| + | IN pCustomerNumber INT, | ||
| + | OUT pCustomerLevel VARCHAR(20)) | ||
| + | BEGIN | ||
| + | DECLARE credit DECIMAL DEFAULT 0; | ||
| + | |||
| + | SELECT creditLimit | ||
| + | INTO credit | ||
| + | FROM customers | ||
| + | WHERE customerNumber = pCustomerNumber; | ||
| + | |||
| + | IF credit > 50000 THEN | ||
| + | SET pCustomerLevel = 'PLATINUM'; | ||
| + | ELSEIF credit <= 50000 AND credit > 10000 THEN | ||
| + | SET pCustomerLevel = 'GOLD'; | ||
| + | ELSE | ||
| + | SET pCustomerLevel = 'SILVER'; | ||
| + | END IF; | ||
| + | END $$ | ||
| + | |||
| + | DELIMITER ; | ||
| + | </pre> | ||
| + | I per provar el procediment: | ||
| + | <pre> | ||
| + | CALL GetCustomerLevel(447, @level); | ||
| + | SELECT @level; | ||
| + | |||
| + | CALL GetCustomerLevel(447, @level); | ||
| + | SELECT @level; | ||
| + | </pre> | ||
| + | |||
=sentència CASE= | =sentència CASE= | ||
*https://www.mysqltutorial.org/mysql-case-statement/ | *https://www.mysqltutorial.org/mysql-case-statement/ | ||
Revisió del 07:21, 28 març 2022
Contingut
IF. Condicionals
Ús del IF-ELSEIF-ELSE:
DELIMITER $$
CREATE PROCEDURE GetCustomerLevel(
IN pCustomerNumber INT,
OUT pCustomerLevel VARCHAR(20))
BEGIN
DECLARE credit DECIMAL DEFAULT 0;
SELECT creditLimit
INTO credit
FROM customers
WHERE customerNumber = pCustomerNumber;
IF credit > 50000 THEN
SET pCustomerLevel = 'PLATINUM';
ELSEIF credit <= 50000 AND credit > 10000 THEN
SET pCustomerLevel = 'GOLD';
ELSE
SET pCustomerLevel = 'SILVER';
END IF;
END $$
DELIMITER ;
I per provar el procediment:
CALL GetCustomerLevel(447, @level); SELECT @level; CALL GetCustomerLevel(447, @level); SELECT @level;
sentència CASE
Bulces LOOP
Bucles WHILE LOOP
Bucles REPEAT LOOP
Sentència LEAVE
creat per Joan Quintana Compte, març 2022