0

我试图重写一个存储过程,我的 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)

更新:我使用了 Diego 推荐的 where 语句,但没有达到预期的结果。它不像 and If\Else scanrio (当我考虑它时这是有道理的)。

我需要首先确定模块 ID 是否为 555,如果是,则仅从硬编码范围内提取日期,否则按书面执行。如果需要更多详细信息,请告诉我。

4

1 回答 1

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'
于 2012-05-01T13:20:18.500 回答