首页 技术 正文
技术 2022年11月20日
0 收藏 449 点赞 2,766 浏览 4309 个字

MySql 存储过程实例

将下面的语句复制粘贴可以一次性执行完,我已经测试过,没有问题!

MySql存储过程简单实例:                                                    
                         /********************* 创建表 *****************************/
                         delimiter //
                                                    
                         DROP TABLE if exists test //
                                                    
                         CREATE TABLE test(
                         id int(11) NULL
                         ) //
                                                    
                        /********************** 最简单的一个存储过程 **********************/
                        drop procedure if exists sp//
                        CREATE PROCEDURE sp() select 1 //
                                                     
                        call sp()//
                                                     
                        /********************* 带输入参数的存储过程  *******************/
                                                    
                        drop procedure if exists sp1 //
                                                    
                        create procedure sp1(in p int)
                        comment ‘insert into a int value’
                        begin
                        /* 定义一个整形变量 */
                        declare v1 int;
                                                      
                        /* 将输入参数的值赋给变量 */
                        set v1 = p;
                                                      
                        /* 执行插入操作 */
                        insert into test(id) values(v1);
                        end
                        //
                                                    
                       /* 调用这个存储过程  */
                       call sp1(1)//
                                                    
                       /* 去数据库查看调用之后的结果 */
                          select * from test//
                                                    
                          /****************** 带输出参数的存储过程 ************************/
                                                    
                          drop procedure if exists sp2 //
                          create procedure sp2(out p int)
                          /*这里的DETERMINISTIC子句表示输入和输出的值都是确定的,不会再改变.我一同事说目前mysql并没有实现该功能,因此加不加都是NOT DETERMINISTIC的*/
                           DETERMINISTIC
                           begin
                           select max(id) into p from test;
                           end
                            //
                                                    
                            /* 调用该存储过程,注意:输出参数必须是一个带@符号的变量 */
                              call sp2(@pv)//
                                                    
                              /* 查询刚刚在存储过程中使用到的变量 */
                                 select @pv//                                                    
                                                    
                              /******************** 带输入和输出参数的存储过程 ***********************/
                                                    
                               drop procedure if exists sp3 //
                               create procedure sp3(in p1 int , out p2 int)
                               begin
                                                    
                               if p1 = 1 then
                               /* 用@符号加变量名的方式定义一个变量,与declare类似 */
                               set @v = 10;
                               else
                               set @v = 20;
                               end if;
                                                      
                               /* 语句体内可以执行多条sql,但必须以分号分隔 */
                                insert into test(id) values(@v);
                                select max(id) into p2 from test;
                                                      
                                end
                                //
                                                    
                                /* 调用该存储过程,注意:输入参数是一个值,而输出参数则必须是一个带@符号的变量 */
                                call sp3(1,@ret)//
                                                    
                                select @ret//
                                                    
                                /***************** 既做输入又做输出参数的存储过程 ******************/
                                                    
                               drop procedure if exists sp4 //
                               create procedure sp4(inout p4 int)
                               begin
                               if p4 = 4 then
                                set @pg = 400;
                                     else
                                     set @pg = 500;
                                     end if; 
                                                       
                                     select @pg;
                                                       
                                     end//
                                                    
                                call sp4(@pp)//
                                                    
                               /* 这里需要先设置一个已赋值的变量,然后再作为参数传入 */
                               set @pp = 4//
                               call sp4(@pp)//
                                                    
                                                    
                               /********************************************************/

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896