Re: Optional Where Parameters on Null Data



"BillCo" <coleman.bill@xxxxxxxxx> wrote in message
news:1177486737.550796.129330@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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


We'll try not to be TOO 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.

I'll pull a Celko and point out you mean columns here. But no matter.



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 don't think you need the WHEN in there, but that's the right basic
approach.

However, a couple of things:

Your nvarchar need sizes, otherwise they're defaulting to one character in
length.

And do you really want to use LIKE in all of those? It'll really hurt
performance in most cases.


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




--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html


.



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: JDBC - Cant get unicode with ResultSet.getString()
    ... The column is a nvarchar and I use ResultSet.getStringto retrieve it. ... >> The data is correct in Sql Server - I viewed it there. ... >> And it is the jdbc getString() that returns it wrong, ...
    (microsoft.public.sqlserver.odbc)
  • Re: Severe performance hit with NCHAR queries
    ... Response time the first couple of times was upwards of 20+ seconds, ... When two different data types meet, SQL Server applies a strict data-type ... The data type of a string literal with a preceeding N is nvarchar. ...
    (comp.databases.ms-sqlserver)
  • Re: slow stored procedure
    ... portion of the table much a condition like WHERE EVT_UPDATED < @CutOFfDate ... WHERE (CAST(ACS_EVENT AS NVARCHAR) + ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Importing Textfiles into SQL Server 2000
    ... I have been unable to import a Text file into SQL Server 2000. ... At the end of the last field of the header column, ... nvarchar NULL, ... datetime NULL, ...
    (microsoft.public.sqlserver.server)