Solved

How to short circuit if value passed in is null, do something if is not null

Posted on 2015-02-09
2
62 Views
Last Modified: 2015-02-09
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'unsp_ApprenticeAdvancementReportLoad')
	DROP PROCEDURE foo.unsp_ApprenticeAdvancementReportLoad
GO

CREATE PROCEDURE foo.unsp_ApprenticeAdvancementReportLoad
(
	@LocNums nvarchar(2000) = null,
	@DateBegin datetime = null,
	@DateEnd datetime = null,
	@SortBy nvarchar(50) = null
)
AS

SELECT DISTINCT mainAppr.LastName
	,mainAppr.FirstName
	,mainAppr.UBCID UBCID	
	,lu.LocalNumber LocalNumber
	,lu.ID LocalUnionID
	,ap.Period
	,ap.PercentOfJourneyWages AdvancementPer	
	,aah.dayAdvancementDate AdvancementDate		
FROM foo.Apprentices mainAppr
	LEFT JOIN foo.ApprenticeAdvancementHistory aah ON aah.ID = mainAppr.CurrentAdvancementID   
	LEFT JOIN foo.ApprenticePeriods ap on aah.ApprenticePeriodID = ap.ID
	LEFT JOIN foo.LocalUnions lu ON lu.ID = mainAppr.LocalUnionID 
	LEFT JOIN foo.ApprenticeStatus apprStatus ON mainAppr.ApprenticeTypeID = apprStatus.ApprenticeTypeID   
WHERE  apprStatus.IsInProgram = 1 
AND ((@LocNums is null) OR lu.LocalNumber IN (UNAdmin.unf_SplitToString(@LocNums, ',')))
AND ((@DateBegin is null) OR (AdvancementDate >= @DateBegin))
AND @DateEnd is null OR AdvancementDate <= @DateEnd
ORDER BY ((@sortby is null) OR @SortBy)

Open in new window



This part:

AND ((@LocNums is null) OR lu.LocalNumber IN (UNAdmin.unf_SplitToString(@LocNums, ',')))
AND ((@DateBegin is null) OR (AdvancementDate >= @DateBegin))
AND @DateEnd is null OR AdvancementDate <= @DateEnd
ORDER BY ((@sortby is null) OR @SortBy)

Open in new window


Keeps saying:

Msg 156, Level 15, State 1, Procedure unsp_ApprenticeAdvancementReportLoad, Line 28
Incorrect syntax near the keyword 'is'.

line 28:

AND ((@DateBegin is null) OR (AdvancementDate >= @DateBegin))


Is my TSQL valid and am I doing this correctly or is there a better way.

[[SQL Server 2008, btw.]]
0
Comment
Question by:Tom Knowlton
2 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40599725
The problem is the ORDER BY clause, not the WHERE clause.

try this please:

ORDER BY ISNULL(@SortBy,'')

{+edit} there is a set of parentheses missing in the where clause

            AND (@DateEnd IS NULL
                  OR AdvancementDate <= @DateEnd)


IF EXISTS (
            SELECT
                  *
            FROM sysobjects
            WHERE type = 'P'
                  AND name = 'unsp_ApprenticeAdvancementReportLoad'
      )
      DROP PROCEDURE foo.unsp_ApprenticeAdvancementReportLoad
GO

CREATE PROCEDURE foo.unsp_ApprenticeAdvancementReportLoad (@LocNums nvarchar(2000) = NULL,
@DateBegin datetime = NULL,
@DateEnd datetime = NULL,
@SortBy nvarchar(50) = NULL)
AS

      SELECT DISTINCT
            MAINAPPR.LastName
          , MAINAPPR.FirstName
          , MAINAPPR.UBCID UBCID
          , LU.LocalNumber LOCALNUMBER
          , LU.ID LOCALUNIONID
          , AP.Period
          , AP.PercentOfJourneyWages ADVANCEMENTPER
          , AAH.dayAdvancementDate ADVANCEMENTDATE
      FROM foo.Apprentices MAINAPPR
            LEFT JOIN foo.ApprenticeAdvancementHistory AAH ON AAH.ID = MAINAPPR.CurrentAdvancementID
            LEFT JOIN foo.ApprenticePeriods AP ON AAH.ApprenticePeriodID = AP.ID
            LEFT JOIN foo.LocalUnions LU ON LU.ID = MAINAPPR.LocalUnionID
            LEFT JOIN foo.ApprenticeStatus APPRSTATUS ON MAINAPPR.ApprenticeTypeID = APPRSTATUS.ApprenticeTypeID
      WHERE APPRSTATUS.IsInProgram = 1
            AND ((@LocNums IS NULL)
                  OR LU.LocalNumber IN (UNAdmin.unf_SplitToString(@LocNums, ',')))
            AND ((@DateBegin IS NULL)
                  OR (AdvancementDate >= @DateBegin))
            AND (@DateEnd IS NULL
                  OR AdvancementDate <= @DateEnd)
ORDER BY ISNULL(@SortBy,'')

Open in new window

0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 40599753
That was it!!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question