數據庫存儲過程

存儲過程

創建多種種存儲過程

(1)不帶參數的存儲過程
eg: 使用create procedure 命令建立名爲StudentCourseScore存儲過程,該存儲過程用於查詢“學生選課名稱、學分及分數”視圖中的數據。寫出程序代碼。

--(1)
use student
go
if exists (select name from sysobjects 
where name='pr_StudentCourseScore' and type = 'P')
drop procedure pr_StudentCourseScore
go
create procedure pr_StudentCourseScore
as
select 課程.課程名稱,課程.學分, 學生成績.分數
from 學生成績,課程
where 學生成績.課程號 = 課程.課程號
go
exec pr_StudentCourseScore
go 


(2)帶輸入參數的存儲過程
eg:使用帶參數的存儲過程StudentAge,根據制定的“年齡”,找出與給定“年齡”相等的學生的“學號”和“姓名”。寫出程序代碼。

use student
go
if exists (select name from sysobjects where name='pr_StudentAge' and type = 'P' )
drop procedure pr_StudentAge
go
create procedure pr_StudentAge @age int
as
select 學號,姓名
from 學生信息
where 年齡=@age
go
exec pr_StudentAge 19
go

(3)帶輸出參數的存儲過程
eg:在上題中設置@count參數,作爲輸出參數,返回和給定“年齡”相同的學生的總人數。寫出程序代碼。

use student
go
if exists (select name from sysobjects where name='pr_StudentAge' and type = 'P' )
drop procedure pr_StudentAge
go
create procedure pr_StudentAge @age int,@count int output
as
select @count = count(*)
from 學生信息
where 年齡=@age
go
declare @Scount int 
exec pr_StudentAge 20,@Scount output --執行時要給實參去接收形參
print '年齡爲19的學生總數爲:'+cast(@Scount as char(4)) 
--cast函數將int型Scount轉化爲char型 ,並返回值
go

(4)加密存儲過程
(5)嵌套使用存儲過程(最多32層)

存儲過程 屬性查看,修改,刪除

(1)查看存儲過程信息
exec sp_helptext 存儲過程名字
(2)修改存儲過程
use 數據庫名字
go
alter procedure 存儲過程名字

重寫一遍存儲過程

(3)刪除存儲過程
drop proceduer 存儲過程名字

利用存儲過程對數據 插入,修改,刪除

(1)插入
eg:使用存儲過程實現向“學生信息”表插入一條記錄的操作。

use student
go
if exists (select name from sysobjects where name = 'pr_InsStuInfo' and type = 'P')
drop procedure pr_InsStuInfo
go
create procedure pr_InsStuInfo @學號 char(7),@姓名 char(20),@性別 char(2),@年齡 int,
@所在系 char(15),@flag int output
as
begin
		set @flag = 1
		insert into 學生信息 values(@學號,@姓名,@性別,@年齡,@所在系)
		if @@ERROR<>0
		set @flag = 0
end
return
go
declare @flag int
set @flag = 0
exec pr_InsStuInfo '008','張三','男',21,'計算機',@flag output
if @flag = 1
print '學生信息添加成功!'
else
print '學生信息添加失敗!'
go

(2)修改
eg:在“學生信息”表中,修改和所給的“學號”相同的記錄,用存儲過程實現。

use student
go
if exists(select name from sysobjects 
		where name = 'pr_UpdStuInfo' and type = 'P')
drop procedure pr_UpdStuInfo
go
create procedure pr_UpdStuInfo @學號 char(7),@姓名 char(20),@性別 char(2),@年齡 int,
@所在系 char(15),@flag int output
as
set @flag = 1
if exists (select * from 學生信息 where 學號 = @學號)
update 學生信息
set 姓名=@姓名,性別=@性別,年齡=@年齡,所在系=@所在系
where 學號 = @學號
else
set @flag = 0
return
go
declare @flag int
set @flag = 0
exec pr_UpdStuInfo '008','張三','男',21,'計算機',@flag output
if @flag = 1
print '學生信息修改成功!'
else
print '學生信息修改失敗!’

go

(3)刪除
eg:在“學生信息”表中,刪除和所給的“學號”相同的記錄,用存儲過程實現。

use student
go
if exists (select name from sysobjects
				where name ='pr_DelStuInfo' and type = 'P')
drop procedure pr_DelStuInfo
go
create procedure pr_DelStuInfo @學號 char(7),@flag int output
as
begin
set @flag = 1
if exists (select * from 學生信息 where 學號 = @學號)
delete 學生信息 where 學號=@學號
else
set @flag = 0
end
return
go
declare @flag int,@學號 char(7)
set @學號 = '008'
set @flag = 0
exec pr_DelStuInfo @學號,@flag output
if @flag = 1
print '該學生信息刪除成功!'
else
print '該學生不存在,無法刪除!'
go


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