Solved

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

Posted on 2015-02-09
2
56 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: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:knowlton
ID: 40599753
That was it!!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now