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可以用变量。
 


 

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