Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-02-09
2
Medium Priority
?
69 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 49

Accepted Solution

by:
PortletPaul earned 2000 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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

876 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