【正文】
t_user,status) values(@id,@user_name,39。提交 39。) GO 存儲(chǔ)過程 ? 刪除存儲(chǔ)過程 use db_Sql GO 判斷存儲(chǔ)過程 pro_prc15是否存在,如果存在將它刪掉 if exists(select name from sysobjects where name=39。pro_prc1539。and type=39。p39。) drop proc pro_prc15 刪除存儲(chǔ)過程 go 存儲(chǔ)過程 ? 重新命名存儲(chǔ)過程 if exists(select name from sysobjects where name=39。pro_person1539。 and type=39。p39。) drop proc pro_per15 go use db_SQL go create procedure pro_Ren15 as select * from tb_Ren15 where 家庭住址 =39。吉林省 39。 go exec sp_rename 39。pro_Ren1539。,39。pro_pRen1539。 go 函數(shù) (標(biāo)量函數(shù) ) ? 創(chuàng)建標(biāo)量函數(shù) 標(biāo)量函數(shù)是指函數(shù)體包含一條或多條 SQL語句,這些語句以 begin開始,并以 end結(jié)束。創(chuàng)建用戶自定義函數(shù)用 create function語句,而創(chuàng)建標(biāo)量函數(shù)需要 create function與 begin……end一起配合使用。 ? 創(chuàng)建標(biāo)量函數(shù)語法 create function 名稱 ([{@參數(shù)名稱 參數(shù)類型 [=默認(rèn)值 ]}[,n]]) returns 返回值類型 [with encryption] [as] begin 函數(shù)體 return 函數(shù)返回值 end 函數(shù) (標(biāo)量函數(shù) ) Eg: create function valid_id (@stu_id char(7)) returns bit as begin declare @returnvalue bit declare @count int set @count=(select count(*) from tbl_stu where stu_id=@stu_id) if @count0 set @returnvalue=1 else set @returnvalue=0 return @returnvalue end 函數(shù) ( 標(biāo)量函數(shù) ) Eg: create function dept_name ( @name varchar(10) ) returns varchar(15) as begin declare @dept_name varchar(15) set @dept_name=(select 所屬部門 from tb_apay08 where 姓名 =@name) return @dept_name end 函數(shù) ( 標(biāo)量函數(shù) ) Eg: create function show_price ( @goods_name varchar(20) ) returns float as begin declare @price float set @price=(select 進(jìn)價(jià) from tb_ware14 where 商品名稱 =@goods_name) return @price end 函數(shù) ( 標(biāo)量函數(shù) ) Eg: create function goods_time ( @id int ) returns datetime as begin declare @goods_time datetime set @goods_time=(select 進(jìn)貨日期 from tb_ware14 where 編號(hào) =@id) return @goods_time end 函數(shù) ( 單語句表值型函數(shù) ) 單語句表值函數(shù)又稱內(nèi)聯(lián)表值函數(shù),這類型函數(shù)以表的形式返回一個(gè)值 , 相當(dāng)于一個(gè)參數(shù)化的視圖。 創(chuàng)建單語句表值型函數(shù)語法 create function 名稱 ([{@參數(shù)名稱 參數(shù)類型 [=默認(rèn)值 ]}[,n]]) returns table [with encryption] [as] return ( select語句) 函數(shù) ( 單語句表值函數(shù) ) Eg: create function show_stu ( @tick char(2) ) returns table as return (select * from tbl_stu where tick=@tick) 函數(shù) (多語句表值型函數(shù) ) 多語句表值型函數(shù)是標(biāo)量函數(shù)和單語句函數(shù)的結(jié)合體,該函數(shù)返回的是一個(gè)表, 可以進(jìn)行多次查詢。 create function 名稱 ([{@參數(shù)名稱 參數(shù)類型 [=默認(rèn)值 ]}[,n]]) returns @局部變量 table [with encryption] [as] begin 函數(shù)體 return 函數(shù)返回值 end 函數(shù) (多語句表值型函數(shù) ) create function show_ginfo ( @id int ) returns @returntable table(商品名稱 varchar(20),售價(jià) float,備注 varchar(20)) as begin declare @goods_name varchar(20),@price float declare @i int set @i=(select count(*) from tb_ware14 where 編號(hào) =@id) if @i0 insert into @returntable(商品名稱 ,售價(jià) ) select 商品名稱 ,售價(jià) from tb_ware14 where 編號(hào) =@id else insert into @returntable(備注 ) values(39。沒有該商品信息 39。) return end 游標(biāo) 游標(biāo)提供一種從表中檢索數(shù)據(jù)并進(jìn)行操作的靈活手段 ,游標(biāo)主要用在服務(wù)器上 ,處理 由客戶端發(fā)給服務(wù)器的 SQL 語句 ,或是批處理、存儲(chǔ)過程、觸發(fā)器中的數(shù)據(jù)處理請(qǐng)求 一個(gè)完整的游標(biāo)由 5部分組成 ,并且這 5個(gè)部分應(yīng)符合下面的順序 : (1) 聲明游標(biāo) (2) 打開游標(biāo) (3) 從一個(gè)游標(biāo)中查找信息 (4) 關(guān)閉游標(biāo) (5) 釋放游標(biāo) 語法 : Declare cursor_name[insensitive|scroll] cursor For select_statement For{Read only|update[of column name[,….n]]}] 游標(biāo) (定義游標(biāo) ) Eg: declare cur_ware cursor for 聲明游標(biāo) select 商品名稱 ,進(jìn)貨日期 ,進(jìn)價(jià) ,售價(jià) from tb_ware14 open cur_ware 打開游標(biāo) declare @curware cursor 創(chuàng)建游標(biāo)變量 set @curware=cur_ware 為游標(biāo)變量賦值 fetch next from @curware 讀取游標(biāo)中的值勤 close cur_ware 關(guān)閉游標(biāo) 游標(biāo) (讀取游標(biāo)中數(shù)據(jù) ) declare emp_cursor cursor for select 姓名 ,性別 from tb_emp06 open emp_cursor declare @name varchar(8),@sex char(2) fetch next from emp_cursor into @name,@sex print 39。姓名 :39。+@name+39。性別 :39。+@sex fetch next from emp_cursor into @name,@sex print 39。姓名 :39。+@name+39。性別 :39。+@sex Close emp_cursor 當(dāng)打開一個(gè)游標(biāo)之后,就可以讀取游標(biāo)中的數(shù)據(jù)了。可以使用 Fetch命令讀取 游標(biāo)中的某一行數(shù)據(jù)。 語法 : Fetch [[next|prior|first|last |absolute{n|@var} |relative{n|@nvar} ] From ]{{[global]cursor_name}|@cursor_variable_name [into @variable_name[,….n]] 游標(biāo) (讀取游標(biāo)中數(shù)據(jù) ) @@fetch_status ?返回值 0: fetch語句成功 ?返回值 1:fetch語句失敗或此行不在結(jié)果集中 ?返回值 2:被提取的行不存在。 Eg: Declare readcursor cursor for Select 編號(hào) ,姓名 ,性別 ,所屬部門 From tb_employee Open readcursor Fetch next from readcursor While @@fetch_status=0 begin fetch next from readcursor end 游標(biāo) (關(guān)閉 /釋放 ) 語法 : Close {{{Global] cursor_name}]|cursor_variable_name} Deallocate {{{Global] cursor_name}]|cursor_variable_name} 使用觸發(fā)器 觸發(fā)器是一種特殊類型的存儲(chǔ)過程 ,是為響應(yīng)數(shù)據(jù)操作語言事件或數(shù)據(jù) 定義語言事件而執(zhí)行的存儲(chǔ)過程 .當(dāng)用戶對(duì)表進(jìn)行相應(yīng)操作時(shí) ,觸發(fā)器將自動(dòng) 執(zhí)行 .觸發(fā)器可以基于表創(chuàng)建 ,也可以基于視圖創(chuàng)建 . 觸發(fā)器是數(shù)據(jù)庫獨(dú)立的對(duì)象 .當(dāng)一個(gè)事件發(fā)生時(shí) ,觸發(fā)器自動(dòng)隱式運(yùn)行 . 但觸發(fā)器不能接收參數(shù) .SQL支持 3種類型的觸發(fā)器 :Insert、 Update、 Delete. 當(dāng)向表插入數(shù)據(jù)、更新數(shù)據(jù)、刪除數(shù)據(jù)時(shí),觸發(fā)器就被調(diào)用。 創(chuàng)建觸發(fā)器的語法 : Create trigger trigger_name On {table|view} [with encryption] { {{for|after|instead of}{[insert][,][update][,][delete]} [not for replication] as [{if update(column) [{and|or}update(column)] [….n] }] Sql_statement[….n] } } 使用觸發(fā)器 ? 觸發(fā)器的優(yōu)點(diǎn) ?觸發(fā)器是自動(dòng)的 :它們?cè)趯?duì)表的數(shù)據(jù)作了任何修改 (比如手工輸入或者應(yīng)用程序采取的操作 )之后立即被激活。 ?觸發(fā)器可以通過數(shù)據(jù)庫中的相關(guān)表進(jìn)行層疊更改。 ?觸發(fā)器可以強(qiáng)制限制,這些限制比用 CHECK 約束所定義的更復(fù)雜。 使用觸發(fā)器 ? 創(chuàng)建 Insert觸發(fā)器 use db_sql go create trigger tri_employee on tb_employee17 after insert as print(39。歡迎加入本公司?。?39。) go insert into tb_employee17 values (39。0800839。,39。鮑艷 39。,39。男 39。,39。開發(fā)部 39。,39。07071639。) 使用觸發(fā)器 ? 創(chuàng)建 Update觸發(fā)器 use db_sql go create trigger tri_employee on tb_employee17 after insert as print(39。歡迎加入本公司?。?39。) go insert into tb_employee17 values (39。0800839。,39。鮑艷 39。,39。男 39。,39。開發(fā)部 39。,39。07071639。) 使用觸發(fā)器 ? 創(chuàng)建 Delete觸發(fā)器 create trigger tri_dele_laborage on tb_laborage17 with encryption after delete as print(39。數(shù)據(jù)刪除成功 39。) go delete from tb_laborage17 where 編號(hào) = 5 ? 刪除觸發(fā)器 使用觸發(fā)器 drop trigger tri_laborage 索引 ? 索引 在對(duì)大量數(shù)據(jù)進(jìn)行查詢時(shí) ,可以應(yīng)用到索引技術(shù) .索