I am trying to create a sproc with multiple order by fields. The order by field and direction is passed through the sproc as arguments.
CREATE PROCEDURE GetFilteredLogs
@FromDate datetime2,
@ToDate datetime2,
@SearchText nvarchar(100) = NULL,
@LogTypeIds Ids READONLY,
@AreaIds Ids READONLY,
@SubTypeIds Ids READONLY,
@UnitIds Ids READONLY,
@SortField nvarchar(25) = NULL,
@SortDirection nvarchar(5) = NULL
AS
SELECT *
FROM LogsView
WHERE (CreatedDate >= @FromDate AND CreatedDate <= @ToDate) AND
(
(
([Text] LIKE '%' + @SearchText + '%' OR @SearchText IS NULL)
AND (LogTypeId IN (SELECT Id FROM @LogTypeIds) OR NOT EXISTS (SELECT 1 FROM @LogTypeIds))
AND (OperationAreaId IN (SELECT Id FROM @AreaIds) OR NOT EXISTS (SELECT 1 FROM @AreaIds))
AND (Subtype IN (SELECT Id FROM @SubTypeIds) OR NOT EXISTS (SELECT 1 FROM @SubTypeIds))
AND (Unit IN (SELECT Id FROM @UnitIds) OR NOT EXISTS (SELECT 1 FROM @UnitIds))
) OR IsCritical = 1
)
ORDER BY
CASE @SortField
WHEN 'LogTypeId' THEN CreatedDate DESC(should be passed as argument), LogTypeId DESC
ELSE CreatedDate DESC
END
GO
The order by section in the above sproc is not in correct syntax. How can I do it and is it possible the way I have tried above?
1条答案
按热度按时间ffvjumwh1#
This is a classic Kitchen Sink Query .
Use dynamic SQL to build up a series of conditions and ordering clauses. Then use
sp_executesql
to execute it, passing in all necessary parameters.It's not clear from your description, but it seems you always want to sort by
CreatedDate
, and possibly also byLogTypeId
depending on the parameter.