【SQLServer系列教程】——存儲過程的創建與使用

1.什麼是存儲過程?

在這裏插入圖片描述
在這裏插入圖片描述

2.存儲過程的優點

在這裏插入圖片描述

3.存儲過程的定義及使用

在這裏插入圖片描述
在這裏插入圖片描述

4.存儲過程的分類

4.1 系統存儲過程

4.1.1 系統存儲過程的介紹

在這裏插入圖片描述

4.1.2 系統存儲過程明細

在這裏插入圖片描述

4.1.3 系統存儲過程的調用

在這裏插入圖片描述

4.2 帶參數的存儲過程

在這裏插入圖片描述

在這裏插入圖片描述

4.3 有輸出返回的存儲過程

在這裏插入圖片描述
在這裏插入圖片描述

5.案例代碼:

示例1:常用系統存儲過程的使用

sp_databases

EXEC  sp_renamedb 'ProductDB','pDB'

USE StudentManager
GO
sp_tables

EXEC sp_columns Students 

EXEC sp_help Students 

EXEC sp_helpconstraint Students

EXEC sp_stored_procedures  

示例2:常用擴展存儲過程的使用

USE master
GO
EXEC xp_cmdshell 'mkdir D:\ProductDB', NO_OUTPUT
IF EXISTS(SELECT * FROM sysdatabases  WHERE name='ProductDB')
   DROP DATABASE ProductDB
GO
--CREATE DATABASE ProductDB
-- (
--  …
--)
--GO
EXEC xp_cmdshell 'dir D:\ProductDB\'   -- 查看文件

示例3:創建、執行無參的存儲過程

use StudentManager
go
if exists(select * from sysobjects where name='usp_ScoreQuery')
drop procedure usp_ScoreQuery
go
create procedure usp_ScoreQuery --創建存儲過程
as
    --查詢考試信息
    select Students.StudentId,StudentName,ClassName,
              ScoreSum=(CSharp+SQLServerDB) from Students
    inner join StudentClass on StudentClass.ClassId=Students.ClassId
    inner join ScoreList on Students.StudentId=ScoreList.StudentId
    order by ScoreSum DESC
    --統計分析考試信息
    select StudentClass.ClassId,C#Avg=avg(CSharp),DBAvg=avg(SQLServerDB)  into #scoreTemp
    from StudentClass 
    inner join Students on StudentClass.ClassId=Students.ClassId
    inner join ScoreList on ScoreList.StudentId=Students.StudentId
    group by StudentClass.ClassId order by ClassId
    select ClassName,C#Avg,DBAvg from #scoreTemp
    inner join StudentClass on StudentClass.ClassId=#scoreTemp.ClassId
go
exec usp_ScoreQuery  --調用存儲過程

示例4:創建、執行有參的存儲過程

use StudentManager
go
if exists(select * from sysobjects where name='usp_ScoreQuery2')
drop procedure usp_ScoreQuery2
go
--創建帶參數的存儲過程
create procedure usp_ScoreQuery2 
@CSharp int,
@DB int
as
    select Students.StudentId,StudentName,C#=CSharp,DB=SQLServerDB
    from Students
    inner join ScoreList on Students.StudentId=ScoreList.StudentId
    where CSharp<@CSharp or SQLServerDB<@DB
go
--調用帶參數的存儲過程
exec usp_ScoreQuery2 60,65 --按照參數順序賦值
exec usp_ScoreQuery2 @DB=65,@CSharp=60 --參數順序可以調換


示例5:創建、執行有默認值參數的存儲過程

use StudentManager
go
if exists(select * from sysobjects where name='usp_ScoreQuery3')
drop procedure usp_ScoreQuery3
go
--創建帶參數的存儲過程
create procedure usp_ScoreQuery3 
@CSharp int=60,
@DB int=60
as
    select Students.StudentId,StudentName,C#=CSharp,DB=SQLServerDB
    from Students
    inner join ScoreList on Students.StudentId=ScoreList.StudentId
    where CSharp<@CSharp or SQLServerDB<@DB
go
--調用帶參數的存儲過程
exec usp_ScoreQuery3 65 --第二個參數沒有賦值,則默認
exec usp_ScoreQuery3 @DB=65
exec usp_ScoreQuery3 default,65 --不使用顯示方式賦值
exec usp_ScoreQuery3   --兩個參數都是用默認參數

示例6:創建帶輸出參數的存儲過程

use StudentManager
go
if exists(select * from sysobjects where name='usp_ScoreQuery4')
drop procedure usp_ScoreQuery4
go
create procedure usp_ScoreQuery4 --創建帶參數的存儲過程
@AbsentCount int output,--缺考總人數
@FailedCount int output,--不及格總人數
@CSharp int=60,
@DB int=60
as
    select Students.StudentId,StudentName,C#=CSharp,DB=SQLServerDB
				 from Students
				 inner join ScoreList on Students.StudentId=ScoreList.StudentId
				 where CSharp<@CSharp or SQLServerDB<@DB        --顯示結果列表 
    select @AbsentCount=count(*) from Students 
				where StudentId not in(select StudentId from ScoreList) --查詢缺考總人數
    select @FailedCount=count(*) from ScoreList
				 where CSharp<@CSharp or SQLServerDB<@DB      --查詢不及格總人數
go

示例9:調用帶輸出參數的存儲過程

use StudentManager
go
--調用帶參數的存儲過程
declare @AbsentCount int,@FailedCount int --首先定義輸出參數
exec usp_ScoreQuery4 @AbsentCount output,@FailedCount output
--使用反饋的結果
select 缺考總數=@AbsentCount,不及格總數=@FailedCount

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章