Mark K
Mark K

Reputation: 591

Stored Procedure with a conditional?

I trying to rewrite a stored procedure and my SQL is not very good. What i'm hoping to do is write it so that if ModuleID is 555 then select a custom date range (eg. 2012-01-01 2012-12-31). The Current SP is below.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO


/*** EventsGetByRange ***/

ALTER PROCEDURE [dbo].[EventsGetByRange]
(
 @Filter nvarchar(500),
 @BeginDate datetime,
 @EndDate datetime
)
AS
SET DATEFORMAT mdy
Declare @sql nvarchar(4000)
Select @sql = 'SELECT E.PortalID, E.EventID, E.RecurMasterID, E.ModuleID, E.EventDateBegin, E.EventDateEnd, '
 + 'E.EventTimeBegin, E.Duration, E.EventName, E.EventDesc, '
 + 'E.Importance, E.CreatedDate,  '
 + 'CreatedBy = U.DisplayName, '
 + 'CreatorID =  E.CreatedBy, '
 + 'E.Every, '
 + 'E.Period, '
 + 'E.RepeatType, '
 + 'E.Notify, '
 + 'E.approved, '
 + 'E.Signups, '
 + 'E.MaxEnrollment, '
 + '(Select count(*) from dbo.EventsSignups WHERE EventID = E.EventID and E.Signups = 1) as Enrolled, '
 + 'E.EnrollRoleID, '
 + 'E.EnrollFee, '
 + 'E.EnrollType, '
 + 'E.PayPalAccount, '
 + 'E.PayPalPassword, '
 + 'E.Cancelled, '
 + 'E.DetailPage, '
 + 'E.DetailNewWin, '
 + 'E.DetailURL, '
 + 'E.ImageURL, '
 + 'E.ImageType, '
 + 'E.ImageWidth, '
 + 'E.ImageHeight, '
 + 'E.ImageDisplay, '
 + 'E.Location, '
 + 'c.LocationName, '
 + 'c.MapURL, '
 + 'E.Category, '
 + 'b.CategoryName, '
 + 'b.Color, '
 + 'b.FontColor, '
 + 'E.Reminder, '
 + 'E.TimezoneOffset, '
 + 'E.SendReminder, '
 + 'E.ReminderTime, '
 + 'E.ReminderTimeMeasurement, '
 + 'E.ReminderFrom, '
 + 'E.SearchSubmitted, '
 + 'E.CustomField1, '
 + 'E.CustomField2, '
 + 'E.EnrollListView, '
 + 'E.DisplayEndDate, '
 + 'E.AllDayEvent, '
 + 'E.OwnerID, '
 + 'OwnerName = O.DisplayName, '
 + 'E.LastUpdatedAt, '
 + 'LastUpdatedBy = L.DisplayName, '
 + 'E.LastUpdatedID, '
 + '(Select ModuleTitle from dbo.Modules WHERE ModuleID = E.ModuleID) as ModuleTitle, '
 + 'RMOwnerID = r.OwnerID, '
 + 'r.RRULE, '
 + 'E.OriginalDateBegin, '
 + 'E.NewEventEmailSent '
 + 'FROM dbo.Events E '
 + 'inner join dbo.EventsRecurMaster AS r on E.RecurMasterID = r.RecurMasterID '
 + 'left outer join dbo.Users U on E.CreatedBy = U.UserID '
 + 'left outer join dbo.Users O on E.OwnerID = O.UserID '
 + 'left outer join dbo.Users L on E.LastUpdatedID = L.UserID '
 + 'left join dbo.EventsCategory b on E.Category = b.Category '
 + 'left join dbo.EventsLocation c on E.Location = c.Location '
 + 'WHERE (E.ModuleID = 555 AND E.EventTimeBegin BETWEEN 2012-01-01 AND 2012-12-31) OR ((E.EventTimeBegin <= DATEADD(DAY,1,''' + convert(varchar, @EndDate) + ''') AND DATEADD(minute,E.Duration,E.EventTimeBegin) >= ''' + convert(varchar, @BeginDate) + ''') OR '
 + '  (E.EventTimeBegin BETWEEN ''' + convert(varchar, @BeginDate) + ''' AND DATEADD(DAY,1,''' + convert(varchar, @EndDate) + ''')))'
 + '  AND E.Approved = 1'
 + '  AND E.Cancelled = 0'
 + ' ' + @Filter + ' '
 + ' ORDER BY E.EventDateBegin, E.EventTimeBegin, E.EventDateEnd'
EXEC (@sql)

UPDATE: I used the where statemnnt that Diego recommended but that is not having the desired result. It does not act as and If\Else scanrio (which makes sense when I think about it).

I need to first identify if the module ID is 555 and if so only pull the dates from in the hard coded range otherwise execute it as written. Please let me know if more detail is required.

Upvotes: 0

Views: 700

Answers (1)

Diego
Diego

Reputation: 36136

  • is the proc failing?
  • did you try adding E.ModuleID = 555 on the where clause?
  • Do you really want to hard code the value 555? how about passing it on a parameter?
  • And most important question: why adding the sql statement to a variable and execute it? why not just run the SQL? Is it because of the @Filter variable?
  • also, sql server 2005 or 2008?
  • why nvarchar and not varchar on your variables ("n" occupies double of space)

EDIT:

ok, you have a OR in there so it may be tricky. Do you want everything from code 555 despite the date range value, or everything within the date range and code 555? I assume option 2 would make more sense so just add

E.ModuleID = 555

before the

 + '  AND E.Approved = 1'

Upvotes: 1

Related Questions