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

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)//
                                                    
                                                    
                                                    /********************************************************/

MySql 存储过程实例(附完整注释)的更多相关文章

  1. MySql 存储过程实例(附完整注释)(转)

    MySql 存储过程实例(附完整注释) 将下面的语句复制粘贴可以一次性执行完,我已经测试过,没有问题! MySql存储过程简单实例:                                   ...

  2. PHP调用MYSQL存储过程实例

    PHP调用MYSQL存储过程实例 标签: mysql存储phpsqlquerycmd 2010-09-26 11:10 11552人阅读 评论(3) 收藏 举报 实例一:无参的存储过程$conn = ...

  3. 调用MYSQL存储过程实例

    PHP调用MYSQL存储过程实例 http://blog.csdn.net/ewing333/article/details/5906887 http://www.cnblogs.com/kkchen ...

  4. MySql 存储过程实例 - 转载

    MySql 存储过程实例 将下面的语句复制粘贴可以一次性执行完,我已经测试过,没有问题! MySql存储过程简单实例:                                          ...

  5. MySQL 存储过程实例 与 ibatis/mybatis/hibernate/jdbc 如何调用存储过程

    虽然MySQL的存储过程,一般情况下,是不会使用到的,但是在一些特殊场景中,还是有需求的.最近遇到一个sql server向mysql迁移的项目,有一些sql server的存储过程需要向mysql迁 ...

  6. mysql 存储过程实例

    --存储过程名和参数,参数中in表示传入参数,out标示传出参数,inout表示传入传出参数 create procedure p_procedurecode(in sumdate varchar(1 ...

  7. MySQL存储过程实例

    一.创建MySQL数据库函数 TCC:无参数,查询fruit表中的所有数据 : TAA:两个参数,查询fruit总共有多少行:查询ids为某个值时水果表的数据 TDD:两个参数,查询ids不等于某个值 ...

  8. Mysql 存储过程实例详解

    存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句的集合,存储和和函数的区别在于函数必须有返回值,而存储过程没有,存储过程的参数可以使用IN.OUT.INOUT类型,而函数的参数只能是IN类 ...

  9. Android 拖动条(SeekBar)实例 附完整demo项目代码

    1.拖动条的事件 实现SeekBar.OnSeekBarChangeListener接口.需要监听三个事件:数值改变(onProgressChanged)开始拖动(onStartTrackingTou ...

随机推荐

  1. js toggle事件

    参数:even (Function): 第奇数次点击时要执行的函数. odd (Function): 第偶数次点击时要执行的函数. 示例:$("p").toggle(functio ...

  2. Linux下编译使用boost库:

    Boost是什么不多说, 下面说说怎样在Linux下编译使用Boost的所有模块. 1. 先去Boost官网下载最新的Boost版本, 我下载的是boost_1_56_0版本, 解压. 2. 进入解压 ...

  3. Floating Action Button(漂浮按钮)

    参考:http://blog.csdn.net/pengkv/article/details/46427891 效果图: 步骤一: 在build.gradle添加以下代码,导入包 dependenci ...

  4. Sklearn库例子2:分类——线性回归分类(Line Regression )例子

    线性回归:通过拟合线性模型的回归系数W =(w_1,…,w_p)来减少数据中观察到的结果和实际结果之间的残差平方和,并通过线性逼近进行预测. 从数学上讲,它解决了下面这个形式的问题:      Lin ...

  5. Netflix Zuul 了解

    Zuul 是提供动态路由,监控,弹性,安全等的边缘服务.Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门.Zuul 可以适当的对多个 Amazon Auto Scal ...

  6. shiro权限验证标签

    实例: spring-shiro.xml /admin/repairType/index = roles["ROLE_ADMIN"] /admin/user=roles[" ...

  7. php发送ssl邮件

    <?php /** * 邮件发送类 * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件(单个或多个附件),支持到服务器的ssl连接 * 需要的php扩展:soc ...

  8. CERT_KEY_CONTEXT_PROP_ID,CERT_KEY_PROV_INFO_PROP_ID,CERT_KEY_PROV_HANDLE_PROP_ID和CERT_KEY_SPEC_PROP_ID

    上面四个属性是CertSetCertificateContextProperty或CertGetCertificateContextProperty可以为证书上下文设置的几个属性,下面对它们的关联简单 ...

  9. UI-程序的运行顺序

    在AppDelegate.m里面的每个方法里都输入(包括main文件里也输入) NSLog(@"%s %d”,__func__,__LINE__); __func__ :代表使用的方法 __ ...

  10. 关于Action<T> 、Func<T>、EventHandler<T>、event、delegate

    c# 最初的时候 只有 delegate,之后的版本封装了Action<T> .Func<T>.EventHandler<T> 关于Action<T>  ...