search
HomeDatabaseMysql TutorialMySQL5.0新特性教程存储过程:第三讲_MySQL

The New SQL Statements 新SQL语句
  
  Variables 变量
  
  在复合语句中声明变量的指令是DECLARE。
  
  (1) Example with two DECLARE statements
  
  两个DECLARE语句的例子
  
  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; // /* I won't CALL this */
  
  在过程中定义的变量并不是真正的定义,你只是在BEGIN/END块内定义了而已(译注:也就是形参)。
  
  注意这些变量和会话变量不一样,不能使用修饰符@你必须清楚的在BEGIN/END块中声明变量和它们的类型。
  
  变量一旦声明,你就能在任何能使用会话变量、文字、列名的地方使用。
  
  (2)Example with no DEFAULT clause and SET statement
  
  没有默认子句和设定语句的例子
  
  CREATE PROCEDURE p9 ()
  
  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; // /* I won't CALL this */
  
  有很多初始化变量的方法。如果没有默认的子句,那么变量的初始值为NULL。你可以在任何时候使用SET语句给变量赋值。
  
  (3)Example with DEFAULT clause
  
  含有DEFAULT子句的例子
  
  CREATE PROCEDURE p10 ()
  
  BEGIN
  
  DECLARE a, b INT DEFAULT 5;
  
  INSERT INTO t VALUES (a);
  
  SELECT s1 * a FROM t WHERE s1 >= b;
  
  END; //
  
  我们在这里做了一些改变,但是结果还是一样的。在这里使用了DEFAULT子句来设定初始值,这就不需要把DECLARE和SET语句的实现分开了。
  
  (4)Example of CALL
  
  调用的例子
  
  mysql> CALL p10() //
  
  +--------+
  
  | s1 * a |
  
  +--------+
  
  | 25 |
  
  | 25 |
  
  +--------+
  
  2 rows in set (0.00 sec)
  
  Query OK, 0 rows affected (0.00 sec)
  
  结果显示了过程能正常工作
  
  (5) Scope
  
  作用域
  
  CREATE PROCEDURE p11 ()
  
  BEGIN
  
  DECLARE x1 CHAR(5) DEFAULT 'outer';
  
  BEGIN
  
  DECLARE x1 CHAR(5) DEFAULT 'inner';
  
  SELECT x1;
  
  END;
  
  SELECT x1;
  
  END; //
  
  现在我们来讨论一下作用域的问题。例子中有嵌套的BEGIN/END块,当然这是合法的。同时包含两个变量,名字都是x1,这样也是合法的。内部的变量在其作用域内享有更高的优先权。当执行到END语句时,内部变量消失,此时已经在其作用域外,变量不再可见了,因此在存储过程外再也不能找到这个声明了的变量,但是你可以通过OUT参数或者将其值指派 给会话变量来保存其值。
  
  调用作用域例子的过程:
  
  mysql> CALL p11()//
  
  +-------+
  
  | x1 |
  
  +-------+
  
  | inner |
  
  +-------+
  
  +-------+
  
  | x1 |
  
  +-------+
  
  | outer |
  
  +-------+
  
  我们看到的结果时第一个SELECT语句检索到最内层的变量,第二个检索到第二层的变量

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor