源網頁
實驗過程:
一、在student資料庫上練習建立並呼叫課堂講授的儲存過程和觸發器。
1.建立乙個instead of觸發器,要求實現一下功能:在t_student表上建立乙個刪除型別的觸發器notallowdelete,當上除記錄時,顯示不允許刪除的提示資訊 use students go
if exists(select name from sysobjects
where name='notallowdelete' and type='tr') drop trigger notallowdelete go
create trigger notallowdelete
if(@@error<>0)---------- @@代表系統變數 set @errorvalue=@@error return @errorvalue end
執行該儲存過程:
declare @nl as int, @num as char(10), @returnvalue as int set @num='s99002'
exec stu_age @num,@nl output
print '學號為'+rtrim(cast(@num as char(10)))+'的學生的年齡是'+cast(@nl as
char(2))+'歲' 執行結果:
3.建立乙個名為stu_info的儲存過程,要求:輸入學號,查詢學生所在的班級、學
生姓名,課程名和選課成績。
create proc stu_info @xh as char(10) as begin
select substring(s_number,4,1) as '班級',s_name as '姓名',c_name as '課程名',score as '
成績'
from t_student inner join t_score
on s_number=s_num inner join course on c_number=c_num where @xh=s_number end go
執行該儲存過程: exec stu_info's99001'
4.求乙個數的階乘(沒有返回值) create proc jiecheng @i as int as
declare @result as int declare @ii as int set @result=1 set @ii=@i while @i>1 begin
set @result=@result*@i
set @i=@i-1 if @i>1 continue else begin print @ii
print rtrim(cast(@ii as char(2)))+'的階乘為:'------該輸出必須使用轉換資料型別
的函式cast,否則就會出現如下錯誤:
print @result end end go
執行該儲存過程: declare @data as int set @data=5
exec jiecheng @data 執行結果: 5
5的階乘為: 120
5.求乙個數的階乘,乙個輸入,乙個輸出。(帶有輸出引數的) create proc jiecheng
@i as int,@result as int output as
declare @ii as int set @result=1 set @ii=@i while @i>0 begin
set @result=@result*@i set @i=@i-1 if @i>1 continue else break end go
執行該儲存過程:
declare @data as int,@sum as int set @data=5
exec jiecheng @data,@sum output
print rtrim(cast(@data as char(2)))+'的階乘為:' print @sum
執行結果為: 5的階乘為: 120
(6)帶有預設值的儲存過程。
輸入學號,查詢學生所在的班級、學生姓名,課程名和選課成績。(stu_info1) create proc stu_info1 @num as char(10)='s99001' as
select substring(s_number,4,1) as '班級', s_name as '姓名', c_name as '課程名', score as '成績'
from t_student inner join t_score
on s_number=s_num inner join course on c_number=c_num where @num=s_number go
執行儲存過程: exec stu_info1 執行結果:
當不輸入指定的學號時,資料庫自動給@num賦值為:s99001
三、在bbs資料庫上設計所需觸發器
發表主帖,使用者積分加10分,版塊發帖量加1。
create trigger publish on bbstopic for insert as
begin
declare @yhid as int
declare @bkid as int
select @yhid=tuid , @bkid=tsid from inserted update bbsuser set upoint=upoint+10 where @yhid=uid update bbssession
set stopiccount=stopiccount+1
where @bkid=sid end
執行插入語句後:
insert
intobbstopic(tid,tsid,tuid,ttopic,tcontents,tclickcount,tflag,tlastclickt,treplycount)
values('6','2','1','買賣商場 ')
執行該操作後,bbsuser表中的uid=1的使用者『火雲邪神』的積分upoint加10;bbssseeion表中的sclickcount加1。
跟帖,使用者積分加1,主帖的回覆數量加1、更新最後回覆
時間,版塊發帖量加1。
create trigger gt on bbsreply
for insert,update as begin
declare @yhid as int,@ztid as int,@bkid as int
select @yhid=ruid,@ztid=rtid,@bkid=rsid from inserted update bbsuser set upoint=upoint+1 where @yhid=uid update bbstopic
set treplycount=treplycount+1 where @ztid=tid
update bbssession
set stopiccount=stopiccount+1 where @bkid=sid end
執行插入語句: insert into
bbsreply(rid,rtid,rsid,ruid,rtopic,rcontents) values('6','2','1','2','就業難','機會不平等')
在bbsuser表中2號使用者的upoint就會加1;在bbstopic表中2號主帖的回覆量(treplycount)就會變為7;在bbssession表中版塊發帖量(stopiccount)就會變為1601.
刪除跟帖,使用者積分減20分,版塊發帖量減1。
create trigger scgt on bbsreply for delete,update as begin
declare @yhid as int,@bkid as int
select @yhid=ruid ,@bkid=rsid from deleted update bbsuser set upoint=upoint-20 where @yhid=uid
update bbssession
set stopiccount=stopiccount-1 where @bkid=sid end
刪除主帖,使用者積分減50分,版塊發帖量減1,刪除所有跟帖。
create trigger sczt on bbstopic for delete,update as begin
declare @yhid as int declare @bkid as int
declare @gtid as int
select @yhid=tuid,@bkid=tsid from deleted update bbsuser set upoint=upoint-50 where @yhid=uid
update bbssession
set stopiccount=stopiccount-1 delete
from bbsreply
where rtid=(select tid from deleted )
T SQL語句建立觸發器
create trigger 觸發器名 on 表或檢視 for after instead of 操作時機 insert,update,delete assql語句 例1 要求 在order test表建立insert觸發器,當向order test表插入一行,如果cust test表中對應 記錄s...
T SQL 儲存過程
1 允許模組化程式設計 2 執行速度快 3 減少網路流通量 4 提高系統安全性 儲存過程分為 系統儲存過程和使用者自定義儲存過程 系統儲存過程 由系統定義,存放在master資料庫中以sp 或者xp 開頭 儲存過程的分為帶引數和不帶引數 帶引數的儲存過程 有 輸入引數和輸出引數,輸出引數在定義時除了...
oracle儲存過程 建立儲存過程語句
一 建立儲存過程語句 語法 create or replace procedure testname argument1 type1,as begin end testname 例子 create orreplace procedure test name arg1 varchar2,arg2 nu...