是SQL语句和控制语句的预编译集合,保存在数据库里。可由应用程序调用执行,而且允许用户声明变量、逻辑控制语句及其他强大的编程功能。
优点:
1.模块化设计
2.执行速度快,效率高
3.减少网络流量
4.具有良好的安全性
分类:
系统存储过程 :以'sp_'开头,存放在resource数据库中,
sp_databases 列出服务器上所有的数据库信息,包括数据库名称和数据库大小
sp_helpdb 报告有关指定数据库或所有数据库信息
sp_ renamedb 更改数据库名称
sp_ tables 返回当前环境下可查询的表或视图的信息
sp_ columns 返回某个表或视图的列信息,包括列的数据类型和长度
sp_ help 返回某个数据库对象的信息
sp_ helpconstraint 查看某个表的约束
sp_ helpindex 查看某个表的索引
sp_ stored_procedures 显示存储过程的列表
sp_password 添加或修改登录账户的密码
sp_helptext 显示默认值,未加密过的存储过程、用户定义的存储过程、触发器、视图的实际版本。
用户自定义存储过程
包括三个部分:
1,输入参数、输出参数
2,在存储过程中执行的T-SQL语句
3,存储过程的返回值
创建语法
1 create proc[edure] 存储过程名 2 [{@参数1 数据类型}[= 默认值][output], 3 ..... 4 {@参数1 数据类型}[= 默认值][output] 5 ] 6 as 7 SQL语句
删除语法
1 drop proc[edure] 存储过程名
eg
1 /**--系统常用的存储过程--**/ 2 exec sp_databases --列出当前系统中的数据库 3 exec sp_renamedb'MyBank','Bank' --改变数据库的名称 4 5 6 EXEC sp_columns Student 7 EXEC sp_help Student 8 EXEC sp_helpconstraint Student 9 EXEC sp_helptext 'view_Student_ Result_Info' 10 EXEC sp_stored_procedures 11 12 13 14 --自定义存储过程 15 use MySchool 16 go 17 /*--检测是否存在,:存储过程存放在sysobjects中--*/ 18 if exists(select * from sysobjects where name = 'usp_GetAverageResult') 19 drop procedure usp_GetAverageResult 20 go 21 ---创建无参的存储过程 22 create procedure usp_GetAverageResult --创建存储过程 23 as 24 declare @subjectNo int --课程编号 25 declare @date datetime --最近考试时间 26 27 select @subjectNo=SubjectNo from Subject where SubjectName= 'Java Logic' 28 select @date = max(ExamDate) from Result inner join Subject on Result.StudentNo=Subject.SubjectName 29 where SubjectName=@subjectNo 30 31 declare @avg decimal(18,2) --平均分变量 32 select @avg = AVG(StudentResult) from Result where ExamDate= @date and SubjectNo = @subjectNo 33 print'平均分'+convert(varchar(5),@avg) 34 35 if(@avg > 70) 36 print'考试成绩:优秀' 37 else 38 print'考试成绩:较差' 39 40 print'-------------------------------------------' 41 print'参加本次考试没有通过的学员:' 42 select StudentName,Student.StudentNo,StudentResult from Student 43 inner join Result on Student.StudentNo = Result.StudentNo 44 where StudentResult<60 and ExamDate=@date and SubjectNo=@subjectNo 45 go
来源:https://www.cnblogs.com/Jason-365/p/8067986.html