MySQL Prepare statement uses

Source: Internet
Author: User
Tags pow stmt

Grammar

[SQL]View PlainCopy
    1. PREPARE statement_name from sql_text/* definition * /
    2. Execute statement_name [USING variable [, variable ...]]/* Execute preprocessing statement */
    3. deallocate PREPARE statement_name/* Delete definition * /


Cases

[SQL]View PlainCopy
  1. Mysql> PREPARE prod from "INSERT into Examlple VALUES (?,?)";
  2. mysql> SET @p=' 1 ';
  3. mysql> SET @q=' 2 ';
  4. Mysql> EXECUTE prod USING @p,@q;
  5. mysql> SET @name=' 3 ';
  6. Mysql> EXECUTE prod USING @p,@name;
  7. mysql> deallocate PREPARE prod;


1. Use a variable to make a table name: simply defining the variable with a set or Declare statement, and then directly as the SQL table name is not possible, MySQL will use the variable name as the table name. In other SQL databases as well, the workaround for MSSQL is to use the entire SQL statement as a variable, with the variable as the table name, and then invoke the statement with sp_executesql. This is not possible before mysql5.0, 5.0 introduced a completely new statement, can achieve similar sp_executesql function (only valid for procedure, function does not support dynamic query):

PREPARE stmt_name from preparable_stmt;
EXECUTE Stmt_name [USING @var_name [, @var_name] ...];
{deallocate | DROP} PREPARE Stmt_name;

In order to have a perceptual understanding, let's give a few small examples below:

[SQL]View PlainCopy
  1. mysql> PREPARE stmt1 from ' SELECT SQRT (Pow (?, 2) + POW (?, 2)) as Hypotenuse ';
  2. mysql> SET @a = 3;
  3. mysql> SET @b = 4;
  4. mysql> EXECUTE stmt1 USING @a, @b;
  5. +------------+   
  6. | hypotenuse |
  7. +------------+   
  8. | 5 |
  9. +------------+   
  10. mysql> deallocate PREPARE stmt1;
  11. mysql> SET @s = ' SELECT SQRT (Pow (?, 2) + POW (?, 2)) as Hypotenuse ';
  12. Mysql> PREPARE stmt2 from @s;
  13. mysql> SET @a = 6;
  14. mysql> SET @b = 8;
  15. mysql> EXECUTE stmt2 USING @a, @b;
  16. +------------+   
  17. | hypotenuse |
  18. +------------+   
  19. | 10 |
  20. +------------+   
  21. mysql> deallocate PREPARE stmt2;

If your MySQL version is 5.0.7 or higher, you can also use it in the LIMIT clause, as in the following example:

[SQL]View PlainCopy
    1. mysql> set @a=1;  
    2. mysql> prepare stmt from < Span class= "string" > "SELECT&NBSP;*&NBSP;FROM&NBSP;TBL&NBSP;LIMIT&NBSP;?";    
    3. mysql> execute  stmt using @a;   
    4. mysql> set  @skip = 1; set  @numrows =5;   
    5. mysql > prepare stmt from  " select * from tbl limit ?, ? ";    
    6. mysql> execute stmt  using  @skip,  @numrows;   


Several points of note using PREPARE:
A:prepare stmt_name from preparable_stmt; A statement is predefined and assigned to Stmt_name, and tmt_name is case insensitive.
B: Even in the PREPARABLE_STMT statement? is represented by a string, and you do not need to be? Enclose them in quotation marks.
C: If the new PREPARE statement uses an existing stmt_name, the original will be released immediately! Even though this new PREPARE statement cannot be executed correctly because of an error.
The scope of D:prepare Stmt_name is that the current client connection session is visible.
E: To release a resource for a predefined statement, you can use the deallocate PREPARE syntax.
F:execute stmt_name syntax, if the stmt_name does not exist, an error will be raised.
G: If you do not explicitly invoke the deallocate PREPARE syntax to release resources when terminating a client connection session, the server will release it on its own.
H: In the predefined statements, CREATE TABLE, DELETE, do, INSERT, REPLACE, SELECT, SET, UPDATE, and most of the SHOW syntax are supported.
I:prepare statements can not be used for stored procedures, custom functions! But starting with MySQL 5.0.13, it can be used for stored procedures and is still not supported in functions!
For more information, refer to: http://www.jb51.net/article/7032.htm

MySQL Prepare statement uses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.