我将 3 个参数传递到我的存储过程中:@Time, @DeptID, @Value
.
@Time
表示:1=过去 24 小时,2=过去一周,3=过去一个月,4=过去一年@DeptID
是各个部门的ID@Value
1=按低位排序,2=按高位排序
这是我当前的代码:
DECLARE @SQL VARCHAR(MAX)
SET @SQL = ('SELECT S.ID, S.[Description], D.Department, S.Value, S.[Date] FROM Suggestions S INNER JOIN Department D ON D.ID = S.DeptID WHERE Approved =1')
IF (@DeptID = 0 AND @Value = 0 AND @Time = 0)
BEGIN
SET @SQL = (@SQL +' ORDER BY [Date] DESC')
END
IF (@Time > 0)
BEGIN
SET @SQL = (CASE WHEN @Time = 1 THEN (@SQL + ' AND [Date] >= DATEADD(DAY, -1, GETDATE()) ORDER BY S.[Date] DESC')
WHEN @Time = 2 THEN (@SQL + ' AND [Date] >= DATEADD(DAY, -7, GETDATE()) ORDER BY S.[Date] DESC' )
WHEN @Time = 3 THEN (@SQL + ' AND [Date] >= DATEADD(DAY, -30, GETDATE()) ORDER BY S.[Date] DESC')
WHEN @Time = 4 THEN (@SQL + ' AND [Date] >= DATEADD(DAY, -365, GETDATE()) ORDER BY S.[Date] DESC') END)
END
IF (@DeptID > 0)
BEGIN
SET @SQL = @SQL + ' AND S.DeptID = @DeptID ORDER BY S.[Date] DESC')
END
IF (@Value > 0)
BEGIN
SET @SQL = (CASE WHEN @Value = 1 THEN (@SQL + ' ORDER BY S.Value DESC')
WHEN @Value = 2 THEN (@SQL + ' ORDER BY S.Value ASC')
当只传入一个参数时这很好,但是当尝试通过两个或多个参数进行过滤时,我遇到了问题......到目前为止,我一直收到错误,因为我在末尾添加了 2 个“Order By”子句我的声明:
ORDER BY S.[Date] DESC ORDER BY S.Value DESC
谁能指出我正确的方向?任何帮助都会非常感激