Вы находитесь на странице: 1из 4

Skip navigation links The world's most popular open source database Login Register

Developer ZoneDownloadsDocumentationMySQL Server MySQL Enterprise MySQL Workbenc h MySQL Cluster Topic Guides Expert Guides Other Docs Archives About Documenta tion Library Table of Contents MySQL 5.6 Manual MySQL 5.5 Manual MySQL 5.1 Manual MySQL 5.0 Manual MySQL 3.23/4.0/4.1 Manual Search manual: MySQL 5.0 Reference Manual :: 12 SQL Statement Syntax :: 12.2 Data Manipulati on Statements :: 12.2.1 CALL Syntax 12.2 Data Manipulation Statements -------------------------------------------------------------------------------12.2.2 DELETE Syntax Section Navigation [Toggle] 12.2 Data Manipulation Statements 12.2.1 CALL Syntax 12.2.2 DELETE Syntax 12.2.3 DO Syntax 12.2.4 HANDLER Syntax 12.2.5 INSERT Syntax 12.2.6 LOAD DATA INFILE Syntax 12.2.7 REPLACE Syntax 12.2.8 SELECT Syntax 12.2.9 Subquery Syntax 12.2.10 UPDATE Syntax 12.2.1. CALL Syntax CALL sp_name([parameter[,...]]) CALL sp_name[()] The CALL statement invokes a stored procedure that was defined previously with C REATE PROCEDURE. As of MySQL 5.0.30, stored procedures that take no arguments can be invoked with out parentheses. That is, CALL p() and CALL p are equivalent. CALL can pass back values to its caller using parameters that are declared as OU T or INOUT parameters. When the procedure returns, a client program can also obt ain the number of rows affected for the final statement executed within the rout ine: At the SQL level, call the ROW_COUNT() function; from the C API, call the m ysql_affected_rows() function. To get back a value from a procedure using an OUT or INOUT parameter, pass the p arameter by means of a user variable, and then check the value of the variable a fter the procedure returns. (If you are calling the procedure from within anothe r stored procedure or function, you can also pass a routine parameter or local r outine variable as an IN or INOUT parameter.) For an INOUT parameter, initialize its value before passing it to the procedure. The following procedure has an OU T parameter that the procedure sets to the current server version, and an INOUT value that the procedure increments by one from its current value: CREATE PROCEDURE p (OUT ver_param VARCHAR(25), INOUT incr_param INT) BEGIN # Set value of OUT parameter SELECT VERSION() INTO ver_param; # Increment value of INOUT parameter SET incr_param = incr_param + 1; END;Before calling the procedure, initialize the variable to be passed as the IN OUT parameter. After calling the procedure, the values of the two variables will

have been set or modified: mysql> SET @increment = 10; mysql> CALL p(@version, @increment); mysql> SELECT @version, @increment; +------------+------------+ @version @increment +------------+------------+ 5.0.25-log 11 +------------+------------+ In prepared CALL statements used with PREPARE and EXECUTE, placeholder support i s available in MySQL 5.0 for IN parameters, but not for OUT or INOUT parameters. To work around this limitation for OUT and INOUT parameters, to forgo the use o f placeholders: Refer to user variables in the CALL statement itself and do not specify them in the EXECUTE statement: mysql> SET @increment = 10; mysql> PREPARE s FROM 'CALL p(@version, @increment)'; mysql> EXECUTE s; mysql> SELECT @version, @increment; +-----------------+------------+ @version @increment +-----------------+------------+ 6.0.7-alpha-log 11 +-----------------+------------+ To write C programs that use the CALL SQL statement to execute stored procedures that produce result sets, the CLIENT_MULTI_RESULTS flag must be enabled. This i s because each CALL returns a result to indicate the call status, in addition to any result sets that might be returned by statements executed within the proced ure. CLIENT_MULTI_RESULTS must also be enabled if CALL is used to execute any st ored procedure that contains prepared statements. It cannot be determined when s uch a procedure is loaded whether those statements will produce result sets, so it is necessary to assume that they will. CLIENT_MULTI_RESULTS can be enabled when you call mysql_real_connect(), either e xplicitly by passing the CLIENT_MULTI_RESULTS flag itself, or implicitly by pass ing CLIENT_MULTI_STATEMENTS (which also enables CLIENT_MULTI_RESULTS). To process the result of a CALL statement executed using mysql_query() or mysql_ real_query(), use a loop that calls mysql_next_result() to determine whether the re are more results. For an example, see Section 19.8.12, C API Support for Multi ple Statement Execution . For programs written in a language that provides a MySQL interface, there is no native method for directly retrieving the results of OUT or INOUT parameters fro m CALL statements. To get the parameter values, pass user-defined variables to t he procedure in the CALL statement and then execute a SELECT statement to produc e a result set containing the variable values. To handle an INOUT parameter, exe cute a statement prior to the CALL that sets the corresponding user variable to the value to be passed to the procedure. The following example illustrates the technique (without error checking) for the stored procedure p described earlier that has an OUT parameter and an INOUT par ameter: mysql_query(mysql, "SET @increment = 10"); mysql_query(mysql, "CALL p(@version, @increment)"); mysql_query(mysql, "SELECT @version, @increment"); result = mysql_store_result(mysql); row = mysql_fetch_row(result); mysql_free_result(result);After the preceding code executes, row[0] and row[1] c ontain the values of @version and @increment, respectively. Previous / Next / Up / Table of Contents User Comments Posted by Arvind Jagtap on January 5 2007 9:12am [Delete] [Edit] Here is an example by using you can pass name as parameter and can get customers

id Step 1. DROP PROCEDURE sp_get_rec; Step 2. CREATE PROCEDURE sp_get_rec(OUT str_id int(3) , match_name CHAR(20) ) BEGIN SELECT id into str_id FROM authors where name=match_name; END Step 3. mysql > call sp_get_rec(@id,'Arvind'); Step 4. mysql> select @id; after doing this it will display id associated with that name if its in DB. Thank you. Add your own comment. Top / Previous / Next / Up / Table of ContentsDeveloper Zone Documentation Developer Articles News & Events Forums Bugs Forge Planet MySQL Labs Downloads MySQL Community Server MySQL Proxy MySQL Cluster MySQL Workbench MySQL Connectors Archives Snapshots Mirrors Documentation MySQL Reference Manuals MySQL Workbench Expert Guides Topic Guides MySQL Cluster Other Documents MySQL University About Archives Support MySQL Meetups Lists Forums Other Legal Contact Us Site Map

2011, Oracle Corporation and/or its affiliates

Вам также может понравиться