Optional Where Parameters on Null Data




I'm new to SQL Server, so if I'm doing anything stupid don't be
mean :)

I have a procedure that I use to return data based on optional
parameters. It works fine, except when the underlying data contains a
null on one if the fields being searched.

My system uses a default wildcard for all parameters, so this excludes
such records. I need a way to add in " OR fldName IS NULL " where the
parameter is empty or '%'. I've looked at using CASE WHEN, but it
doesnt seem to like SQL Keywords being part of the WHEN clause.

I'd hate to have to resort to executing concatonated strings made from
IF and ELSE statements. Just too messy and not at all pretty!

Any Ideas? Here's what I've got:

ALTER PROCEDURE [dbo].[procFindUnits]
@strUnitID nvarchar = '%',
@strProjectName nvarchar = '%',
@strAddress nvarchar = '%',
@strTenancy nvarchar = '%',
@strTenure nvarchar = '%'
AS
BEGIN

SET NOCOUNT ON;

SELECT tblUnits.strUnitID,
tblProjects.strProjectName,
qryAddresses.Address_OneLine,
lkpTenancyTypes.strTenancyType,
lkpTenureTypes.strTenureType

FROM tblUnits INNER JOIN
tblProjects ON tblUnits.intProjectID = tblProjects.intProjectID
LEFT OUTER JOIN
lkpTenancyTypes ON tblUnits.intTenancyType =
lkpTenancyTypes.intTenancyType LEFT OUTER JOIN
lkpTenureTypes ON tblUnits.intTenureType =
lkpTenureTypes.intTenureTypeID LEFT OUTER JOIN
qryAddresses ON tblUnits.strUnitID = qryAddresses.strUnitID

WHERE (tblUnits.strUnitID LIKE @strUnitID)
AND (tblProjects.strProjectName LIKE @strProjectName)
AND (qryAddresses.Address_OneLine LIKE @strAddress)
AND (lkpTenancyTypes.strTenancyType LIKE @strTenancy)
AND (lkpTenureTypes.strTenureType LIKE @strTenure)
END

.



Relevant Pages

  • Re: Optional Where Parameters on Null Data
    ... I'm new to SQL Server, so if I'm doing anything stupid don't be ... @strProjectName nvarchar = '%', ... lkpTenancyTypes ON tblUnits.intTenancyType = ... lkpTenancyTypes.intTenancyType LEFT OUTER JOIN ...
    (comp.databases.ms-sqlserver)
  • Re: Optional Where Parameters on Null Data
    ... I'm new to SQL Server, so if I'm doing anything stupid don't be ... Your nvarchar need sizes, otherwise they're defaulting to one character in ... lkpTenancyTypes ON tblUnits.intTenancyType = ... lkpTenancyTypes.intTenancyType LEFT OUTER JOIN ...
    (comp.databases.ms-sqlserver)
  • Re: SqlCommand.ExecuteReader does not populate Output Parmeters?
    ... He still thinks it's stupid. ... > SQL Server executes statements and how that information flows between the ... > client and the server. ... > verify that output parameters really come at the end of the results at the ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Upsizing issue
    ... >>> half the application because of stupid Recordset problem. ... You could link the SQL Server ... magnitude as modifying the original ASP pages. ...
    (microsoft.public.inetserver.asp.db)
  • Re: SqlCommand.ExecuteReader does not populate Output Parmeters?
    ... It is stupid and you don't understand the issue. ... The issue here has nothing to do with SQL server the issue is that the DataReader interface is not reading the output parameter until after the recordset has been paged through. ... The problem is that setting an OUTPUT parameter to a value like @@ROWCOUNT is meaningless. ...
    (microsoft.public.dotnet.framework.adonet)