Home Database Mysql Tutorial MYSQL入门学习之十二:存储过程的基本操作_MySQL

MYSQL入门学习之十二:存储过程的基本操作_MySQL

Jun 01, 2016 pm 01:37 PM
database expression

bitsCN.com

MYSQL入门学习之十二:存储过程的基本操作

 

相关链接:

MYSQL入门学习之一:基本操作

http:///database/201212/173868.html

MYSQL入门学习之二:使用正则表达式搜索

http:///database/201212/173869.html

MYSQL入门学习之三:全文本搜索

http:///database/201212/173873.html

MYSQL入门学习之四:MYSQL的数据类型

http:///database/201212/175536.html

MYSQL入门学习之五:MYSQL的字符集

http:///database/201212/175541.html

MYSQL入门学习之六:MYSQL的运算符

http:///database/201212/175862.html

MYSQL入门学习之七:MYSQL常用函数

http:///database/201212/175864.html

MYSQL入门学习之八:数据库及表的基本操作

http:///database/201212/175867.html

MYSQL入门学习之九:索引的简单操作

http:///database/201212/176772.html

MYSQL入门学习之十:视图的基本操作

http:///database/201212/176775.html

MYSQL入门学习之十一:触发器的基本操作

http:///database/201212/176781.html

 

存储过程简单来说,就是为以后的使用而保存的一条或多条MySQL语句的集合。可将其视为批文件,虽然它们的作用不仅限于批处理。    

        使用存储过程需要MySQL5及以后的版本支持。

一、为什么要使用存储过程

        通过把处理封闭在容易使用的单元中,简化复杂的操作;

        将一系列处理步骤放到同一存储过程中,保证了数据的完整性和操作的安全性;

        简化对变更的管理;

        提高性能。使用存储过程比使用单独的SQL语句要快;

        存在一些只能用在单个请求中的MySQL元素和特性,存储过程可以使用它们来编写功能更强更灵活的代码;

二、基本操作

1、创建存储过程

        CREATE PROCEDURE sp_name ([proc_parameter[,...]])

            [characteristic ...] routine_body

            proc_parameter:

            [ IN | OUT | INOUT ] param_name type

        示例:

[sql] 

mysql>create procedure sp_test()  

    ->begin  

    ->    select userid,username from newname where userid=215;  

    ->end  

    ->//  

 

2、执行存储过程

        CALL sp_name;

        示例:

[sql] 

mysql> call sp_test();  

+--------+----------+  

| userid | username |  

+--------+----------+  

|    215 | NULL     |  

+--------+----------+  

 

3、删除存储过程

        DROP PROCEDURE [ IF EXISTS ] sp_name;

        示例:

[sql] 

mysql> drop procedure if exists sp_test;  

 

4、查看存储过程创建信息

        SHOW CREATE PROCEDURE sp_name;

        示例:

[sql] 

mysql> show create procedure sp_test;  

+-----------+----------+--------------------------------------------------------+----------------------+----------------------+--------------------+  

| Procedure | sql_mode | Create Procedure                                       | character_set_client | collation_connection | Database Collation |  

+-----------+----------+--------------------------------------------------------+----------------------+----------------------+--------------------+  

| sp_test   |          | CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_test`()  

begin  

    select userid,username from newname where userid=215;  

end | latin1               | latin1_swedish_ci    | latin1_swedish_ci  |  

+-----------+----------+--------------------------------------------------------+----------------------+----------------------+--------------------+  

 

5、查看存储过程状态

        SHOW PROCEDURE STATUS [ LIKE '' ];

        示例:

[sql] 

mysql> show procedure status like 'sp_test';  

+------+---------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+-  

| Db   | Name    | Type      | Definer        | Modified            | Created             | Security_type | Comment | character_set_client |  

+------+---------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+-  

| test | sp_test | PROCEDURE | root@localhost | 2012-12-17 23:57:38 | 2012-12-17 23:57:38 | DEFINER       |         | latin1               |  

+------+---------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+-  

6、使用存储过程参数

        示例:

[sql] 

mysql> delimiter //  

mysql> create procedure sp_type_cnt(  

    ->     IN in_type int,  

    ->     OUT out_cnt int  

    -> )  

    -> begin  

    ->     select count(*)  

    ->     from newname  

    ->     where type = in_type  

    ->     into out_cnt;  

    -> end;  

    -> //  

mysql> delimiter ;  

mysql> call sp_type_cnt(0,@cnt);  

mysql> select @cnt;  

+------+  

| @cnt |  

+------+  

|  159 |  

+------+  

 

bitsCN.com
Statement of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
How to solve discuz database error How to solve discuz database error Nov 20, 2023 am 10:10 AM

The solutions to discuz database error are: 1. Check the database configuration; 2. Make sure the database server is running; 3. Check the database table status; 4. Back up the data; 5. Clear the cache; 6. Reinstall Discuz; 7. Check the server resources ; 8. Contact Discuz official support. Solving Discuz database errors requires starting from multiple aspects, gradually identifying the cause of the problem, and taking corresponding measures to repair it.

Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Jun 15, 2024 pm 09:49 PM

IntelArrowLakeisexpectedtobebasedonthesameprocessorarchitectureasLunarLake,meaningthatIntel'sbrandnewLionCoveperformancecoreswillbecombinedwiththeeconomicalSkymontefficiencycores.WhileLunarLakeisonlyavailableasava

Can't drop database 'database_name'; database doesn't exist - How to solve MySQL error: Can't drop database, database doesn't exist Can't drop database 'database_name'; database doesn't exist - How to solve MySQL error: Can't drop database, database doesn't exist Oct 05, 2023 am 11:46 AM

How to solve MySQL error: Unable to delete database, database does not exist Overview: MySQL is a commonly used relational database management system. When using MySQL, we often need to manage the database, including creating databases, deleting databases and other operations. However, when deleting a database, sometimes you will encounter the error message "Can'tdropdatabase'database_name';databasedoesn'texist", that is, you cannot delete it.

How to solve Python expression syntax errors? How to solve Python expression syntax errors? Jun 24, 2023 pm 05:04 PM

Python, as a high-level programming language, is easy to learn and use. Once you need to write a Python program, you will inevitably encounter syntax errors, and expression syntax errors are a common one. In this article, we will discuss how to resolve expression syntax errors in Python. Expression syntax errors are one of the most common errors in Python, and they are usually caused by incorrect usage of syntax or missing necessary components. In Python, expressions usually consist of numbers, strings, variables, and operators. most common

Java Lambda Expression in Practice: Unlocking the Mysteries of Functional Programming with Code Java Lambda Expression in Practice: Unlocking the Mysteries of Functional Programming with Code Feb 26, 2024 am 10:25 AM

Lambda expression, as the name suggests, is an anonymous function with the arrow symbol (->) as its core. It allows you to pass blocks of code as arguments to other methods, or store them into variables for later use. Lambda expression syntax is concise and easy to understand, and it is very suitable for processing data flow and parallel computing. 1. The basic syntax of Lambda expression The basic syntax of Lambda expression is as follows: (parameter list)->{code block} Among them, the parameter list and code block are optional. If there is only one parameter, the parentheses can be omitted. If the code block is only one line, the curly braces can be omitted. For example, the following code block uses a Lambda expression to add 1 to a number: List

In C and C++, comma is used to separate expressions or statements In C and C++, comma is used to separate expressions or statements Sep 09, 2023 pm 05:33 PM

In C or C++, the comma "," has different uses. Here we will learn how to use them. Commas as operators. The comma operator is a binary operator that evaluates the first operand, discards the result, then evaluates the second operand and returns the value. The comma operator has the lowest precedence in C or C++. Example #include<stdio.h>intmain(){ intx=(50,60); inty=(func1(),func2());} Here 60 will be assigned to x. For the next statement, func1( will be executed first

Introduction to exponential function expressions in C language Introduction to exponential function expressions in C language Feb 18, 2024 pm 01:11 PM

Introduction to how to write exponential function expressions in C language and code examples What is an exponential function? The exponential function is a common type of function in mathematics. It can be expressed in the form of f(x)=a^x, where a is the base and x is the exponent. . Exponential functions are mainly used to describe exponential growth or exponential decay. Code example of exponential function In C language, we can use the pow() function in the math library to calculate the exponential function. The following is a sample program: #include

lambda expression in Java lambda expression in Java Jun 09, 2023 am 10:17 AM

Lambda expressions in Java With the release of Java 8, lambda expressions have become one of the most concerned and discussed topics among Java developers. Lambda expressions can simplify Java programmers' tedious writing methods, and can also improve the readability and maintainability of programs. In this article, we will take a deep dive into lambda expressions in Java and how they provide a simpler and more intuitive programming experience in Java code.

See all articles