Anda di halaman 1dari 6

MYSQL CREATE PROCEDURE p () -> BEGIN -> /* This procedure does nothing */ -> END;// A Definition and an Example

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database). A stored procedure has a name, a parameter list, and an SQL statement, which can contain many more SQL statements. CREATE PROCEDURE procedure1 (IN parameter1 INTEGER) BEGIN IF parameter1 = 17 THEN SET variable1 = 'birds'; ELSE SET variable1 = 'beasts'; END IF; END /* assignment */ /* end of IF */ /* end of block */ DECLARE variable1 CHAR(10); /* name */

/* parameters */ /* start of block */ /* variables */ /* start of IF */ /* assignment */

INSERT INTO table1 VALUES (variable1);/* statement */

DELIMITER // For example: mysql> DELIMITER // The delimiter is the character or string of characters that you'll use to tell the mysql client thatyou've finished typing in an SQL statement. For ages, the delimiter has always been a semicolon. That causes a problem because, in a stored procedure, one can have many

statements, and every one must end with a semicolon. DELIMITER ;//. CREATE PROCEDURE Example CREATE PROCEDURE p1 () SELECT * FROM t; // Perhaps this is the first stored procedure that you've ever made with MySQL. If so, be sure to mark this event in your diary. CREATE PROCEDURE p1 () SELECT * FROM t; // "CREATE PROCEDURE". CREATE PROCEDURE p1 () SELECT * FROM t; // The New SQL Statements ---------------------Variables The statement you use to define variables in a compound statement is DECLARE. (1) Example with two DECLARE statements CREATE PROCEDURE p8 () BEGIN DECLARE a INT; DECLARE b INT; SET a = 5; SET b = 5; INSERT INTO t VALUES (a); SELECT s1 * a FROM t WHERE s1 >= b; END; // You don't really define variables within the stored procedure. You define them within the BEGIN/END block. (2) Example with no DEFAULT clause and SET statement CREATE PROCEDURE p9 () <-<-The first part of the SQL statement that creates a stored procedure is the words

BEGIN DECLARE a INT /* there is no DEFAULT clause */; DECLARE b INT /* there is no DEFAULT clause */; SET a = 5; /* there is a SET statement */ SET b = 5; /* there is a SET statement */ INSERT INTO t VALUES (a); SELECT s1 * a FROM t WHERE s1 >= b; END; // There are several ways to initialize a variable. When declared without a DEFAULT clause, the initial value of a variable is always NULL. You can use the SET statement to assign another value (3) Example with DEFAULT clause CREATE PROCEDURE p10 () BEGIN DECLARE a, b INT DEFAULT 5; INSERT INTO t VALUES (a); SELECT s1 * a FROM t WHERE s1 >= b; END; // Here's a variation that does the same thing. This time I'm putting both variable declarations on the same line and using a DEFAULT clause to set the initial value, rather than doing two separate DECLARE and SET statements. (4) Example of CALL mysql> CALL p10() // IF statements: If condition then Statements End if;

If condition then Statements Else statements End if; Loops WHILE ... END WHILE CREATE PROCEDURE p14 () BEGIN DECLARE v INT; SET v = 0; WHILE v < 5 DO INSERT INTO t VALUES (v); SET v = v + 1; END WHILE; END; // LOOP ... END LOOP CREATE PROCEDURE p16 () BEGIN DECLARE v INT; SET v = 0; loop_label: LOOP INSERT INTO t VALUES (v); SET v = v + 1; IF v >= 5 THEN LEAVE loop_label; END IF; END LOOP; END; // REPEAT ... END REPEAT CREATE PROCEDURE p15 () BEGIN DECLARE v INT; SET v = 0; REPEAT INSERT INTO t VALUES (v);

SET v = v + 1; UNTIL v >= 5 END REPEAT; END; //

REPEAT ... END REPEAT: look at the UNTIL CREATE PROCEDURE p15 () BEGIN DECLARE v INT; SET v = 0; REPEAT INSERT INTO t VALUES (v); SET v = v + 1; UNTIL v >= 5 END REPEAT; END; //

GOTO CREATE PROCEDURE p... BEGIN ... LABEL label_name; ... GOTO label_name; ... END; Functions Summary: CREATE FUNCTION Limitations of functions CREATE FUNCTION factorial (n DECIMAL(3,0)) RETURNS DECIMAL(20,0) BEGIN DECLARE factorial DECIMAL(20,0) DEFAULT 1; DECLARE counter DECIMAL(3,0);

SET counter = n; factorial_loop: REPEAT SET factorial = factorial * counter; SET counter = counter - 1; UNTIL counter = 1 END REPEAT; RETURN factorial; END // 2. Examples INSERT INTO t VALUES (factorial(pi)) // SELECT s1, factorial (s1) FROM t // UPDATE t SET s1 = factorial(s1) WHERE factorial(s1) < 5 //

Anda mungkin juga menyukai