Stored Procedures and Functions
Transcript
Stored Procedures and Functions
Stored Procedures and Functions Dichiarazione di procedure e funzioni CREATE [DEFINER = { user | CURRENT_USER }] PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic ...] routine_body CREATE [DEFINER = { user | CURRENT_USER }] FUNCTION sp_name ([func_parameter[,...]]) RETURNS type [characteristic ...] routine_body Dichiarazione di procedure e funzioni proc_parameter: [ IN | OUT | INOUT ] param_name type func_parameter: param_name type type: Any valid MySQL data type characteristic: LANGUAGE SQL | [NOT] DETERMINISTIC | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA } | SQL SECURITY { DEFINER | INVOKER } | COMMENT 'string' routine_body: Valid SQL procedure statement Definizione di procedure e funzioni [begin_label:] BEGIN [statement_list] END [end_label] BEGIN ... END syntax is used for writing compound statements, which can appear within stored programs. A compound statement can contain multiple statements, enclosed by the BEGIN and END keywords. statement_list represents a list of one or more statements, each terminated by a semicolon (;) statement delimiter Ritorno di una funzione RETURN expr The RETURN statement terminates execution of a stored function and returns the value expr to the function caller. Chiamata di una procedura CALL sp_name([parameter[,...]]) CALL sp_name[()] The CALL statement invokes a stored procedure that was defined previously with CREATE PROCEDURE. As of MySQL 5.0.30, stored procedures that take no arguments can be invoked without parentheses. That is, CALL p() and CALL p are equivalent. Dichiarazione di variabili locali DECLARE var_name [, var_name] ... type [DEFAULT value] This statement is used to declare local variables within stored programs. To provide a default value for the variable, include a DEFAULT clause. The value can be specified as an expression; it need not be a constant. If the DEFAULT clause is missing, the initial value is NULL. Dichiarazione di Conditions DECLARE condition_name CONDITION FOR condition_value condition_value: SQLSTATE [VALUE] sqlstate_value | mysql_error_code Dichiarazione di handler DECLARE handler_type HANDLER FOR condition_value [, condition_value] ... statement handler_type: CONTINUE | EXIT | UNDO The DECLARE ... HANDLER statement specifies handlers that each may deal with one or more conditions. If one of condition_value: these conditions occurs, the SQLSTATE [VALUE] sqlstate_value specified statement is | condition_name executed. | SQLWARNING | NOT FOUND | SQLEXCEPTION | mysql_error_code Assegnazione SET var_name = expr [, var_name = expr] ... The SET statement in stored programs is an extended version of the general SET statement. Assegnazione SELECT col_name [, col_name] ... INTO var_name [, var_name] ... table_expr SELECT ... INTO syntax enables selected columns to be stored directly into variables. The query should return a single row. If the query returns no rows, a warning with error code 1329 occurs (No data), and the variable values remain unchanged. If the query returns multiple rows, error 1172 occurs. Use LIMIT 1 to limit the result set to a single row. Cursors A cursor allows access to the result of a select row by row. Cursors are supported inside stored procedures and functions and triggers. The syntax is as in embedded SQL. Cursors in MySQL have these properties: • Asensitive: The server may or may not make a copy of its result table • Read only: Not updatable • Nonscrollable: Can be traversed only in one direction and cannot skip rows Cursors must be declared before declaring handlers. Variables and conditions must be declared before declaring either cursors or handlers. Utilizzo di cursori DECLARE cursor_name CURSOR FOR select_statement OPEN cursor_name This statement opens a previously declared cursor. FETCH cursor_name INTO var_name [, var_name] ... This statement fetches the next row (if a row exists) using the specified open cursor, and advances the cursor pointer. CLOSE cursor_name This statement closes a previously opened cursor. Costrutto IF IF search_condition THEN statement_list [ELSEIF search_condition THEN statement_list] ... [ELSE statement_list] END IF implements a basic conditional construct. If the search_condition evaluates to true, the corresponding SQL statement list is executed. If no search_condition matches, the statement list in the ELSE clause is executed. Each statement_list consists of one or more statements. Costrutto CASE CASE case_value WHEN when_value THEN statement_list [WHEN when_value THEN statement_list] ... [ELSE statement_list] END CASE Or: CASE WHEN search_condition THEN statement_list [WHEN search_condition THEN statement_list] ... [ELSE statement_list] END CASE The CASE statement for stored programs implements a complex conditional construct. If a search_condition evaluates to true, the corresponding SQL statement list is executed. If no search condition matches, the statement list in the ELSE clause is executed. Ciclo LOOP [begin_label:] LOOP statement_list END LOOP [end_label] LOOP implements a simple loop construct, enabling repeated execution of the statement list. Ciclo REPEAT … UNTIL [begin_label:] REPEAT statement_list UNTIL search_condition END REPEAT [end_label] The statement list within a REPEAT statement is repeated until the search_condition is true. Thus, a REPEAT always enters the loop at least once. Ciclo WHILE [begin_label:] WHILE search_condition DO statement_list END WHILE [end_label] The statement list within a WHILE statement is repeated as long as the search_condition is true. LEAVE and ITERATE LEAVE label This statement is used to exit the flow control construct that has the given label. It can be used within BEGIN ... END or loop constructs (LOOP, REPEAT, WHILE). ITERATE label ITERATE can appear only within LOOP, REPEAT, and WHILE statements. ITERATE means “do the loop again.” Trigger CREATE [DEFINER = { user | CURRENT_USER }] TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_stmt A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. You cannot associate a trigger with a TEMPORARY table or a view. Trigger trigger_time is the trigger action time. It can be BEFORE or AFTER to indicate that the trigger activates before or after each row to be modified. trigger_event indicates the kind of statement that activates the trigger. it can be one of the following: INSERT: The trigger is activated whenever a new row is inserted into the table; UPDATE: The trigger is activated whenever a row is modified; DELETE: The trigger is activated whenever a row is deleted from the table; Event CREATE [DEFINER = { user | CURRENT_USER }] EVENT [IF NOT EXISTS] event_name ON SCHEDULE schedule [ON COMPLETION [NOT] PRESERVE] [ENABLE | DISABLE | DISABLE ON SLAVE] [COMMENT 'comment'] DO sql_statement; schedule: AT timestamp [+ INTERVAL interval] ... | EVERY interval [STARTS timestamp [+ INTERVAL interval] ...] [ENDS timestamp [+ INTERVAL interval] ...] interval: quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE | DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}