mysql存储过程

来源:互联网 发布:农村淘宝加盟申请 编辑:程序博客网 时间:2024/06/15 08:58

一、存储过程与函数的区别:

  1.一般来说,存储过程实现的功能要复杂一点,而函数的实现的功能针对性比较强。

  2.对于存储过程来说可以返回参数(output),而函数只能返回值或者表对象。

  3.存储过程一般是作为一个独立的部分来执行,而函数可以作为查询语句的一个部分来调用,由于函数可以返回一个表对象,因此它可以在查询语句中位于FROM关键字的后面。

 

二、存储过程的优点:

  1.执行速度更快 – 在数据库中保存的存储过程语句都是编译过的

  2.允许模块化程序设计 – 类似方法的复用

  3.提高系统安全性 – 防止SQL注入

  4.减少网络流通量 – 只要传输存储过程的名称

 

系统存储过程一般以sp开头,用户自定义的存储过程一般以usp开头

 

三、定义存储过程语法,"["   里面的内容表示可选项

  create proc 存储过程名

  @参数1        数据类型 [=默认值] [output],

  @参数2        数据类型 [=默认值] [output],

  ...

  as

  SQL语句

 

四、简单的一个例子

  定义存储过程:

  create proc usp_StudentByGenderAge

  @gender nvarchar(10) [='男'],

  @age int [=30]

  as

  select * from MyStudent where FGender=@gender and FAge=@age

 

  执行存储过程:

  Situation One(调用默认的参数):

  exec usp_StudentByGenderAge

  Situation Two(调用自己指定的参数):

  exec usp_StudentByGenderAge '女',50

  或者指定变量名        exec usp_StudentByGenderAge @age=50,@gender='女'

 

  对存储过程进行修改

  alter proc usp_StudentByGenderAge

  @gender nvarchar(10) [='男'],

  @age int [=30],

  --加output表示该参数是需要在存储过程中赋值并返回的

  @recorderCount int output 

  as

  select * from MyStudent where FGender=@gender and FAge=@age

  set @recorderCount=(select count(*) from MyStudent where FGender=@gender and FAge=@age)

 

--output参数的目的,就是调用者需要传递一个变量进来,然后在存储过程中为该变量完成赋值工作,存储过程执行完成以后,将执行的对应结果返回给传递进来的变量。(与C#中的out原理一模一样)

 

调用(记住这里的语法!)因为该存储过程前面还有其他参数,所以要把 @recorderCount写上,该存储过程执行后,相当与完成了以上的查询工作,同时将查询结果得到的条数赋值给了@count变量。(@count是当做参数传给usp_StudentByGenderAge,当存储过程执行完毕以后,将得到的条数返回给@count)

  declare @count int

  exec usp_StudentByGenderAge @recorderCount=@count output

  print @count

 

五、使用存储过程完成分页

1、存储过程代码

    create proc usp_page

  @page int,          ---一页显示多少条记录

    @number int,   ---用户选择了第几页数据

      as

   begin

   select * from

   --小括号里面内容是专门得到排列好的序号

   (

     select  ROW_NUMBER() over(order by(Fid))  as number

     from MyStudent

   ) as t

   where t.number>= (@number-1)*@page+1 and t.number<=@number*@page

   end

0 0
原创粉丝点击