I have a form which is used for searching for records in the database. There is no gaurantee which fields will be filled in on the form when the search commences. Right now I have the query set up to join tables and return all rows to a temp table then I have a conditional for each field to test wether it is null. If it isn't I delete the records that don't have that value for that specific field. Is there a more concise way to do this? Here's a sample of what I'm doing. It's in ANSI SQL but it should be difficult to see what I'm doing:
Code:
Insert into #tmpNickNameOnly(blnDeleted,LINK)
Select 0, m.LINK from MNAMES m
Left Join Aliases a on a.LINK = m.LINK and a.Alias = @Nickname
where @FName is null OR SUBSTRING(m.NAME,POSITION(',' IN m.NAME)+2,30) LIKE @FName + '%'
AND m.LINK <> 0;
If @LName is not null then
Delete #tmpNickNameOnly set blnDeleted = 1 where LINK NOT IN (Select LINK from mnames where SUBSTRING(NAME,1,POSITION(',' IN NAME)-1) = @LName);
End If;
If @DOBBegin is not null then
Delete #tmpNickNameOnly set blnDeleted = 1 where LINK NOT IN (Select LINK from mnames where DOB Between @DOBBegin and @DOBEnd);
End If;
If @Sex is not null then
Delete #tmpNickNameOnly set blnDeleted = 1 where LINK NOT IN (Select LINK from mnames where sex = @Sex);
End If;
If @Race is not null then
Delete #tmpNickNameOnly set blnDeleted = 1 where LINK NOT IN (Select LINK from mnames where Race = @Race);
End If;