存储过程

一、              存储过程概念

存储过程(procedure)类似于C语言中的函数,它是SQL语句和控制流程语句的预编译集合。

 

存储过程(procedure)的优点:

1 允许模块化设计院

2 允许更书报地执行

3 减少网络流量

4 可作为安全机制使用



二、              存储过程的分类

存储过程分为三类:

1 系统存储过程:以sp_开头,类似于C语言中的系统函数。系统存储过程位于master数据库中。

 

2 系统扩展存储过程:以xp_开头,类似于C语言中的系统函数。系统扩展存储过程位于master数据库中。

 

3 自定义存储过程:类似于C语言中的自定义函数。

 

三、              常用系统存储过程

1 sp_helpdb:报告数据库的信息

    execute sp_helpdb           --查看所有数据库信息

    execute sp_helpdb '数据库名' --查看当前数据库信息

 

2 sp_help:查看某个数据库中表的信息

    execute sp_help          --查看某个数据库中表的所有信息

    execute sp_help '表名' --查看某个数据库中单个表的信息

 

3 sp_helpfile:查看当前数据库文件的.mdf和.ldf的位置

    execute sp_helpfile

 

4 sp_helpindex:查看某个表的索引

    execute sp_helpindex '表名'

 

5 sp_helpconstraint:查看某个表的约束

    execute sp_helpconstraint '表名'

 

6 sp_helptext:显示未加密的存储过程触发器或视图的实际文本.

    execute sp_helptext '存储过程|触发器|视图'

 

7 sp_stored_procedures:返回当前数据库中的存储过程的列表.

    execute sp_stored_procedures

 

8 sp_tables:查看当前环境下可查询的对象的列表

    execute sp_tables

    execute sp_tables '表名'

 

9 sp_columns:查看表中列的信息

    execute sp_columns '表名'

 

10 sp_databases:列出服务器上所有的数据库

    execute sp_databases

 

11 sp_renamedb:更改数据库的名字

    execute sp_renamedb '原数据库名','新数据库名'

 

12 sp_password:设置登录帐户的密码

    execute sp_password '旧密码','新密码','登录名'

    alter login '登录名' enable --启用帐户

 

    execute sp_password 'sb','sbsb','sa'

    alter login sa enable

 

 

四、              常用系统扩展存储过程

execute xp_cmdshell 'dos命令' [,no_output]

 

execute xp_cmdshell 'mkdir d:/stu',no_output

execute xp_cmdshell 'dir d:/stu'

 

五、              自定义存储过程

语法:

create procedure 存储过程名

    [{@参数数据类型}[=默认值][output]

    ......,

    {@参数n 数据类型}[=默认值][output]]

as

    SQL 语句

省略output则视为输入参数.

 

1 不带参数的存储过程

 

存储过程_数据库use stuDB

存储过程_数据库go

存储过程_数据库if exists(select * from sysobjects where name='proc_stu')

存储过程_数据库    drop procedure proc_stu

存储过程_数据库go

存储过程_数据库_06/*--创建存储过程--*/

存储过程_数据库create procedure proc_stu

存储过程_数据库as

存储过程_数据库    declare @writtenAvg float,@labAvg float

存储过程_数据库    select @writtenAvg=avg(WrittenExam),@labAvg=avg(LabExam) from stuMarks

存储过程_数据库    print '笔试平均分: '+convert(varchar(5),@writtenAvg)

存储过程_数据库    print '机试平均分: '+cast(@labAvg as varchar(5))

存储过程_数据库    if(@writtenAvg>70 and @labAvg>70)

存储过程_数据库        print '本班考试成绩: 优秀'

存储过程_数据库    else

存储过程_数据库        print '本班考试成绩: 差'

存储过程_数据库        print '-------------------------------------'

存储过程_数据库        print '     参加本次考试没有通过的学生     '

存储过程_数据库        select stuName,stuInfo.stuNo,writtenExam,labExam from stuInfo 

存储过程_数据库            inner join stuMarks on stuInfo.stuNo=stuMarks.stuNo

存储过程_数据库                where writtenExam<60 or labExam<60

存储过程_数据库

存储过程_数据库--调用存储过程--

存储过程_数据库execute proc_stu

存储过程_数据库

存储过程_数据库

 

 

 

2 带输入参数的存储过程

 

存储过程_数据库
存储过程_数据库use stuDB
存储过程_数据库go
存储过程_数据库if exists(select * from sysobjects where name='proc_stu')
存储过程_数据库    drop procedure proc_stu
存储过程_数据库go
存储过程_数据库_06/*--创建存储过程--*/
存储过程_数据库create procedure proc_stu
存储过程_数据库@writtenPass int=60,    --输入参数,笔试及格线
存储过程_数据库@labPass int=60            --输入参数,机试及格线
存储过程_数据库as
存储过程_数据库    print '-------------------------------------'
存储过程_数据库    print '     参加本次考试没有通过的学生     '
存储过程_数据库    select stuName,stuInfo.stuNo,writtenExam,labExam from stuInfo 
存储过程_数据库        inner join stuMarks on stuInfo.stuNo=stuMarks.stuNo
存储过程_数据库            where writtenExam<@writtenPass or labExam<@labPass
存储过程_数据库go
存储过程_数据库
存储过程_数据库--调用存储过程--
存储过程_数据库execute proc_stu                --笔试和机试都采用默认值
存储过程_数据库execute proc_stu 50                --笔试及格线,机试采用默认值.
存储过程_数据库execute proc_stu @labPass=55    --机试及格线,笔试采用默认值.
存储过程_数据库execute proc_stu 65,65            --都不采用默认值
存储过程_数据库
存储过程_数据库

 

 

3 带输出参数的存储过程

存储过程_数据库use stuDB
存储过程_数据库go
存储过程_数据库if exists(select * from sysobjects where name='proc_stu')
存储过程_数据库    drop procedure proc_stu
存储过程_数据库go
存储过程_数据库_06/*--创建存储过程--*/
存储过程_数据库create procedure proc_stu
存储过程_数据库@notpassSum int output,    --指出是输出参数
存储过程_数据库@writtenPass int=60,    --输入参数,笔试及格线,默认参数放后.
存储过程_数据库@labPass int=60            --输入参数,机试及格线,默认参数放后.
存储过程_数据库as
存储过程_数据库    print '笔试平均分: '+convert(varchar(5),@writtenPass)
存储过程_数据库    print '机试平均分: '+cast(@labPass as varchar(5))
存储过程_数据库    print '-------------------------------------'
存储过程_数据库    print '     参加本次考试没有通过的学生     '
存储过程_数据库    select stuName,stuInfo.stuNo,writtenExam,labExam from stuInfo 
存储过程_数据库        inner join stuMarks on stuInfo.stuNo=stuMarks.stuNo
存储过程_数据库            where writtenExam<@writtenPass or labExam<@labPass
存储过程_数据库_06    /*------统计不有通过考试的学员人数------*/
存储过程_数据库    select @notpassSum=count(stuNo) from stuMarks
存储过程_数据库        where writtenExam<@writtenPass or labExam<@labPass
存储过程_数据库go
存储过程_数据库
存储过程_数据库_06/**--调用存储过程--**/
存储过程_数据库
存储过程_数据库--定义变量,用于存放调用存储过程时返回的结果--
存储过程_数据库declare @sum int    
存储过程_数据库--调用时也带output,笔试及格线为,机试及格线默认为            
存储过程_数据库execute proc_stu @sum output,64 
存储过程_数据库select  '未通过人数'+ cast(@sum as varchar(5))+'人'
存储过程_数据库

 

 

4         raiserror处理错误信息

存储过程_数据库use stuDB

存储过程_数据库go

存储过程_数据库if exists(select * from sysobjects where name='proc_stu')

存储过程_数据库    drop procedure proc_stu

存储过程_数据库go

存储过程_数据库_06/*--创建存储过程--*/

存储过程_数据库create procedure proc_stu

存储过程_数据库@notpassSum int output,    --指出是输出参数

存储过程_数据库@writtenPass int=60,    --输入参数,笔试及格线,默认参数放后.

存储过程_数据库@labPass int=60            --输入参数,机试及格线,默认参数放后.

存储过程_数据库as

存储过程_数据库_06    /*------------错误处理----------------*/

存储过程_数据库    if(not @writtenPass between 0 and 100)or(not @labPass between 0 and 100)

存储过程_数据库        begin

存储过程_数据库            raiserror('及格线错误,请指定-100之间的数,统计中断退出!',16,1)

存储过程_数据库            return --立即返回,退出存储过程

存储过程_数据库        end

存储过程_数据库

存储过程_数据库    print '笔试平均分: '+convert(varchar(5),@writtenPass)

存储过程_数据库    print '机试平均分: '+cast(@labPass as varchar(5))

存储过程_数据库    print '-------------------------------------'

存储过程_数据库    print '     参加本次考试没有通过的学生     '

存储过程_数据库    select stuName,stuInfo.stuNo,writtenExam,labExam from stuInfo 

存储过程_数据库        inner join stuMarks on stuInfo.stuNo=stuMarks.stuNo

存储过程_数据库            where writtenExam<@writtenPass or labExam<@labPass

存储过程_数据库_06    /*------统计不有通过考试的学员人数------*/

存储过程_数据库    select @notpassSum=count(stuNo) from stuMarks

存储过程_数据库        where writtenExam<@writtenPass or labExam<@labPass

存储过程_数据库go

存储过程_数据库

存储过程_数据库_06/**--调用存储过程--**/

存储过程_数据库

存储过程_数据库--定义变量,用于存放调用存储过程时返回的结果--

存储过程_数据库declare @sum int,@t int    

存储过程_数据库

存储过程_数据库--调用时也带output,笔试及格线为,机试及格线默认为            

存储过程_数据库execute proc_stu @sum output,800    --大于报错

存储过程_数据库set @t=@@ERROR                        --raiserror报错误后@@ERROR将不等于,表示有错

存储过程_数据库if @t<>0

存储过程_数据库    print '@@ERROR的值是: '+convert(varchar(5),@t)

存储过程_数据库    return --退出批处理,后续语句不执行.

存储过程_数据库select  '未通过人数'+ cast(@sum as varchar(5))+'人'

存储过程_数据库go

存储过程_数据库