有条件的存储过程?

时间:2012-05-01 13:10:11

标签: sql-server stored-procedures

我试图重写存储过程,我的SQL不是很好。我希望做的是写它,如果ModuleID是555,那么选择一个自定义日期范围(例如2012-01-01 2012-12-31)。当前SP低于。

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)

更新:我使用了迭戈推荐的但没有达到预期效果的地方。它不起作用和If \ Else scanrio(当我考虑它时这是有道理的。)

我需要首先确定模块ID是否为555,如果是,则仅从硬编码范围中提取日期,否则按写入方式执行。如果需要更多细节,请告诉我。

1 个答案:

答案 0 :(得分:1)

  • 是proc失败了吗?
  • 您是否尝试在where子句中添加E.ModuleID = 555?
  • 你真的想要硬编码值555吗?如何传递它 参数?
  • 最重要的问题:为什么要将sql语句添加到 变量并执行它?为什么不只是运行SQL?是因为 @Filter变量?
  • 还有,sql server 2005或2008?
  • 为什么nvarchar而不是变量上的varchar(“n”占用双倍 (空间)

编辑:

好吧,你在那里有一个OR,所以它可能很棘手。除了日期范围值,还是日期范围内的所有内容和代码555,您是否想要代码555中的所有内容? 我认为选项2更有意义所以只需添加

E.ModuleID = 555

之前的

 + '  AND E.Approved = 1'