Sql Server數據庫開發之觸發器及CharIndex系統函數的用法

觸發器:
觸發器是數據庫表的觸發器,主要是在對錶的插入、修改、刪除等操作時執行的一段代碼,用來作爲該表的約束。
實例說明:
在一個問題上報案例中,當一個問題事件上報到數據庫表中時,即在數據庫表插入數據時,判斷問題狀態是0(審覈)時,添加一個該問題事件的索引到另一個表中供查詢使用。
代碼如下:

ALTER   TRIGGER [dbo].[InsertProblem] ON  [dbo].[Problem]
AFTER INSERT 
AS 
declare @in_taskid varchar(13);
declare @in_areaid varchar(10);
declare @temp_coordinate varchar(10);
declare @temp_location nvarchar(30);
declare @temp_status varchar(10);
declare @temp_count int;

select @temp_status=status from inserted;
--set @in_areaid='100000';
set @temp_count=0;
if( @temp_status='0')
	begin
	 --根據座標計算得到areaid郵編
	 select @temp_coordinate= coordinate from inserted;
	 select @temp_location= location from inserted;
		/**
		begin	
		declare @area nvarchar(30);  
		declare myCusor cursor for select area from Area ; 
		open myCusor  
		fetch next from myCusor into @area  
		while (@@fetch_status = 0)  
		begin  try
			
			--select @temp_count=count(*) from Area where (CHARINDEX('懷柔',@temp_location)<>0);
			set @temp_count=CHARINDEX('懷柔',@temp_location);
			if(@temp_count<>0) 
			begin
			select @in_areaid=id from Area where area=@area ;
			end 
			fetch next from myCusor into @area  
		
		end  try
		begin catch
		print @@error;
		end catch
		close myCusor  
		deallocate myCusor  
		end
		*/
		select * from Area;
		
			begin
				if @temp_count=0
					select @temp_count= count(*) from Area where (charindex('通州',@temp_location)<>0);
					if @temp_count<>0 
					select @in_areaid= id from Area where area='通州區';
				if @temp_count=0
					select @temp_count= count(*) from Area where (charindex('平谷',@temp_location)<>0);
					if @temp_count<>0 
					select @in_areaid= id from Area where area='平谷區';
				if @temp_count=0
					select @temp_count= count(*) from Area where (charindex('順義',@temp_location)<>0);
					if @temp_count<>0 
					select @in_areaid= id from Area where area='順義區';
				if @temp_count=0
					select @temp_count= count(*) from Area where (charindex('懷柔',@temp_location)<>0);
					if @temp_count<>0 
					select @in_areaid= id from Area where area='懷柔區';
				if @temp_count=0
					select @temp_count= count(*) from Area where (charindex('密雲',@temp_location)<>0);
					if @temp_count<>0 
					select @in_areaid= id from Area where area='密雲縣';
				if @temp_count=0
					select @temp_count= count(*) from Area where (charindex('延慶',@temp_location)<>0);
					if @temp_count<>0 
					select @in_areaid= id from Area where area='延慶縣';
				if @temp_count=0
					select @temp_count= count(*) from Area where (charindex('昌平',@temp_location)<>0);
					if @temp_count<>0 
					select @in_areaid= id from Area where area='昌平區';				
				if @temp_count=0
					select @temp_count= count(*) from Area where (charindex('門頭溝',@temp_location)<>0);
					if @temp_count<>0 
					select @in_areaid= id from Area where area='門頭溝區';	
				if @temp_count=0
					select @temp_count= count(*) from Area where (charindex('房山',@temp_location)<>0);
					if @temp_count<>0 
					select @in_areaid= id from Area where area='房山區';	
				if @temp_count=0
					select @temp_count= count(*) from Area where (charindex('大興',@temp_location)<>0);
					if @temp_count<>0 
					select @in_areaid= id from Area where area='大興區';	
				if @temp_count=0
					set @in_areaid='100000';
	
			
			end
			
		
	 --得到任務號
	 select @in_taskid=pid from inserted;
	 insert into Task values(@in_taskid,@in_areaid,'1',null);
	end

代碼說明:
1. ALTER   TRIGGER [dbo].[InsertProblem] ON  [dbo].[Problem]
 AFTER INSERT
 AS  begin end
以上是觸發器的定義。插入操作之後觸發。
2.select @temp_coordinate= coordinate from inserted;
查詢當前插入的語句中的數據。
3.charindex('通州',@temp_location) 
系統函數,查詢@temp_location中‘通州’所在的位置,不存在返回0.返回值是int型。

---------------------------------------------------------
CharIndex的使用方法:
CHARINDEX( expression1 ,expression2 [ , start_location ] )
如果 expression1 或 expression2 之一是 Unicode 數據類型(nvarchar 或 nchar)而另一個不是,則將另一個轉換爲 Unicode 數據類型。CHARINDEX 不能與 text、ntext 和 image 數據類型一起使用。
如果 expression1 或 expression2 之一爲 NULL,並且數據庫兼容級別爲 70 或更高,則 CHARINDEX 將返回 NULL。如果數據庫兼容級別爲 65 或更低,則 CHARINDEX 將僅在 expression1 和 expression2 都爲 NULL 時才返回 NULL 值。
如果在 expression2 內找不到 expression1,則 CHARINDEX 返回 0。

實際使用時遇到的問題:
在上面觸發器中,使用了遊標,但是在上面的使用中,charindex(@temp1,@temp2) 時,兩個參數都是變量時在程序中(觸發器或存儲過程)並不會執行,該問題不知道什麼原因,直接跳轉到其他位置不執行,
但是該用法拿出來在sql中單獨執行時卻可以,不知道是不是bug。所以上面就全部寫出來了,沒有使用循環遍歷或者遊標的使用。
所以,總結在程序中使用charindex函數時,@temp最好用‘123’這樣的常量,@temp2可以用變量。
 


 

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