求一条SQL语句,组合模糊查询
假设有一个表A,里面有datetime类型字段时间T,char型字段名称N,我想在存储过程里面实现按名称和时间组合查询,其中名称为模糊查询,为空时查询全部,时间匹配到月份,即查询某年某月的记录,为空时查询全部,输入参数假设为@n和@t。 
 谢谢!
------解决方案------------------------创建存储过程 
 create proc spTest @n varchar(10),@t varchar(10) 
 as 
 declare @sql varchar(1000) 
 set @sql =  'select * from table where 1=1  ' +  
 case when isnull(@n, ' ') =  ' ' then  ' ' else  ' and N like  ' '% ' + @n +  '% ' ' ' end +  
 case when isnull(@t, ' ') =  ' ' then  ' ' else  ' and convert(varchar(7),T,120) =  ' ' ' + @t +  ' ' ' ' end 
 EXEC(@sql) 
 GO 
 ----执行存储过程 
 declare @n varchar(10),@t varchar(10) 
 set @n =  ' ' 
 set @t =  '2007/08 ' 
 EXEC spTest @n,@t
------解决方案--------------------case 用在此处还是首次见到,学习了...   
 use pubs 
 go 
 create table A (t datetime,n char(20)) 
 insert into a select getdate() , 'admin ' 
 union all select getdate()+10, 'administrator ' 
 union all select getdate()+30, 'guest test '   
 go   
 ----创建存储过程 
 create proc spTest @n varchar(10),@t varchar(10) 
 as 
 declare @sql varchar(1000) 
 set @sql =  'select * from a where 1=1  ' +  
 case when isnull(@n, ' ') =  ' ' then  ' ' else  ' and N like  ' '% ' + @n +  '% ' ' ' end +  
 case when isnull(@t, ' ') =  ' ' then  ' ' else  ' and convert(varchar(7),T,120) =  ' ' ' + @t +  ' ' ' ' end 
 EXEC(@sql) 
 GO 
 ----执行存储过程 
 declare @n varchar(10),@t varchar(10) 
 set @n =  'administrator ' 
 set @t =  '2007-08 ' 
 EXEC spTest @n,@t   
 drop table a 
 drop proc sptest