在表的多列上搜索查询?

时间:2014-04-28 11:22:27

标签: sql sql-server-2008 search

我们在像这样的表上实现搜索

   create proc prcSearchMember 
(
@UserId uniqueidentifier,
@MemberFirstName varchar(20),
@MemberMiddleName varchar(20),
@MemberLastName varchar(20),
@FamilyHeadName varchar(50),
@FatherName varchar(50),
@MotherName varchar(50),
@DOB datetime,
@GotraID int,
@SectID int,
@BloodGroupID int,
@EducationLevelID int, 
@EducationFieldID int,
@HouseNumber varchar(20),
@StreetName varchar(50),
@Area varchar(50),
@LandMark varchar(50),
@StateID int,
@CountryID int,
@CityID int, 
@PhoneNumber varchar(15),
@EmailAddress varchar(50),
@MaritalStatus varchar(20),
@OccupationTypeID int,
@Gender varchar(10),
@IsSubmit bit
)
as 
if(@MemberFirstName is not null )
 Begin  
Select MemberFirstName,UserId from  MemberInfo
 where MemberFirstName  like  @MemberFirstName
 end
else if (@MemberMiddleName is not null )
Begin  
Select MemberFirstName,UserId from  MemberInfo 
where MemberMiddleName like @MemberMiddleName
end
else if (@MemberMiddleName is not null ) 
Begin    
Select MemberFirstName,UserId from  MemberInfo   where   MemberLastName  like @MemberLastName
end
else if (@MemberLastName is not null ) 
Begin   
Select MemberFirstName,UserId from   MemberInfo   where FamilyHeadName  like @MemberLastName  
end
else if (@MemberLastName is not null ) 
Begin   
Select MemberFirstName,UserId from   MemberInfo   where  FatherName  like @FatherName 
end
else if (@MemberLastName is not null ) 
Begin   
Select MemberFirstName,UserId from   MemberInfo   where MotherName  like @MotherName 
end
else if (@DOB is not null ) Begin   
Select MemberFirstName,UserId from   MemberInfo   where DOB like @DOB
end
else if (@GotraID is not null ) 
Begin    
Select MemberFirstName,UserId from   MemberInfo   where  GotraID like @GotraID
end
else if (@SectID is not null ) Begin    
Select MemberFirstName,UserId from   MemberInfo   where SectID=@SectID
end
else if (@BloodGroupID is not null ) Begin    
Select MemberFirstName,UserId from   MemberInfo   where BloodGroupID like @BloodGroupID  
end
else if (@EducationLevelID is not null ) Begin    
Select MemberFirstName,UserId from   MemberInfo   where EducationLevelID=@EducationLevelID 
end
else if (@EducationFieldID is not null ) Begin    
Select  MemberFirstName,UserId from   MemberInfo   where EducationFieldID like EducationFieldID
end
else if (@HouseNumber is not null ) Begin    
Select MemberFirstName,UserId from   MemberInfo   where HouseNumber like @HouseNumber  
end
else if (@StreetName is not null ) Begin    
Select MemberFirstName,UserId from   MemberInfo   where StreetName like @StreetName  
end
else if (@Area is not null ) Begin    
Select MemberFirstName,UserId from   MemberInfo   where Area like @Area  
end
else if (@LandMark is not null ) Begin    
Select MemberFirstName,UserId from   MemberInfo   where LandMark like @LandMark  
end
else if (@CountryID is not null ) Begin    
Select MemberFirstName,UserId from MemberInfo where CountryID like @CountryID
end
else if (@StateID is not null ) 
Begin  
select MemberFirstName,UserId from   MemberInfo   where StateID like @StateID 
end
else if (@CityID is not null ) Begin    
Select MemberFirstName,UserId from   MemberInfo   where CityID like CityID 
end
else if (@OccupationTypeID is not null ) Begin    
Select  MemberFirstName,UserId from   MemberInfo   where  OccupationTypeID like @OccupationTypeID 
end
else if (@PhoneNumber is not null ) Begin    
Select  MemberFirstName,UserId from   MemberInfo   where  PhoneNumber like @PhoneNumber 
end
else if (@EmailAddress is not null ) 
Begin     
Select MemberFirstName,UserId from   MemberInfo   where EmailAddress  like @EmailAddress 
 end
else if (@MaritalStatus is not null )
 Begin     
Select MemberFirstName,UserId from   MemberInfo   where MaritalStatus  like @MaritalStatus 
 end
else if (@Gender is not null ) 
Begin     
Select MemberFirstName,UserId from  MemberInfo   where Gender like @Gender 
end
else if (@UserId is not null ) Begin    
Select MemberFirstName,UserId from  MemberInfo   where  UserId like @UserId

这些都是单个表的列

所以,如果有任何方法以这种方式实现这样的搜索查询

foreach (column_name in INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'YourTableName')
   select primary_key_coumn 
   from table_name 
   where column_name like @column_name

其中@column_name是特定column

的值

4 个答案:

答案 0 :(得分:1)

我不确定你想要实现的是什么。你在问“写这个搜索有什么好办法吗?”。如果是的话,那么是的,绝对的。据我所知,每个IF分支选择相同的列,因此如果您确保所有存储的proc参数在声明中默认为NULL,那么您可以执行以下操作:

SELECT MemberFirstName, UserId 
FROM  MemberInfo
WHERE (MemberFirstName  LIKE @MemberFirstName OR @MemberFirstName IS NULL)
AND (MemberMiddleName LIKE @MemberMiddleName OR @MemberMiddleName IS NULL)
AND (MemberLastName  LIKE @MemberLastName OR @MemberLastName  IS NULL)
AND --You get the picture by now. Keep adding your conditions here.

优化器非常智能,不会打扰任何评估为NULL的条件(这就是为什么你必须确保参数默认为NULL)。

请注意,您的许多分支将永远不会被执行,因为您有:

else if (@MemberLastName is not null ) 
Begin   
Select MemberFirstName,UserId from   MemberInfo   where FamilyHeadName  like @MemberLastName  
end
else if (@MemberLastName is not null ) 
Begin   
Select MemberFirstName,UserId from   MemberInfo   where  FatherName  like @FatherName 
end
else if (@MemberLastName is not null ) 
Begin   
Select MemberFirstName,UserId from   MemberInfo   where MotherName  like @MotherName 
end

只会执行第一个分支。

我不确定您为什么要询问主键列以及为什么要迭代表中的列以获取主键列?在单个表中,该表的主键列对于您检查的表中的每个列名都是相同的。这会给你带来什么不同?您要解决的实际问题是什么?值得指出的是,自SQL Server 2005以来,建议您使用系统目录视图而不是INFORMATION_SCHEMA,并且它们使用起来非常简单。

答案 1 :(得分:1)

尝试以下方法,

DECLARE @ColName varchar(126) = 'col_name'
DECLARE @ColVal varchar(64) = 'col_val'

DECLARE @SQL AS nvarchar(MAX)
SET @SQL = ''

SELECT @SQL = @SQL + 'SELECT * FROM ' + IST.TABLE_SCHEMA + '.' + IST.TABLE_NAME 
                       +' where '+ @ColName +' = '''+ @ColVal +''' ' + CHAR(13)
FROM 
    INFORMATION_SCHEMA.TABLES IST
    INNER JOIN INFORMATION_SCHEMA.COLUMNS ISC ON IST.TABLE_NAME = ISC.TABLE_NAME
WHERE
    ISC.COLUMN_NAME = @ColName

EXEC (@SQL)

答案 2 :(得分:1)

你可以让你的情况回归到同义反复 从未评估重言式的条件,如

SELECT MemberFirstName
     , UserId 
FROM   MemberInfo
WHERE  MemberFirstName like MemberFirstName

相当于

SELECT MemberFirstName
     , UserId 
FROM   MemberInfo

可以使用NULLCOALESCE在参数为ISNULL时使条件成为重言式的方式更改查询。

对于前几个条件,它就像是

SELECT MemberFirstName, UserId 
FROM   MemberInfo
WHERE  MemberFirstName  like COALESCE(@MemberFirstName, MemberFirstName)
  AND  MemberMiddleName like COALESCE(@MemberMiddleName, MemberMiddleName)
  AND  MemberLastName   like COALESCE(@MemberLastName, MemberLastName)
  AND  FamilyHeadName   like COALESCE(@FamilyHeadName, FamilyHeadName)

答案 3 :(得分:0)

您可以根据搜索参数构建动态SQL。

请参阅以下链接中的示例2.0:Building Dynamic SQL in Stored Procedure