Based on http://dbforums.com/arch/29/2003/3/733876 I have written the following stored procedure. The point is to carry out sorting on the database server, as opposed to inside the application - thus, I need to pass the query the column-name to sort by, and the direction.The following is just with sorting, not bi-directional:CREATE PROCEDURE GetCompanies @Criteria NVARCHAR(50), @inName BIT, @inDescription BIT, @inRelatedProjects BIT, @SortExpr VARCHAR(30), @SortASC BITASDECLARE @SearchStr nvarchar(52)IF @Criteria != '' BEGIN SET @SearchStr = '%' + @Criteria + '%'END ELSE BEGIN SET @SearchStr = '%'ENDSELECT c.CompanyID, c.Name, c.Description, c.RelatedProjects, c.StampFROM tCompany cWHERE @Criteria = '' OR ( (@inName = 1 AND c.Name LIKE @SearchStr) OR (@inDescription = 1 AND c.Description LIKE @SearchStr) OR (@inRelatedProjects = 1 AND c.RelatedProjects LIKE @SearchStr) )ORDER BY CASE @sortExpr WHEN 'name' THEN c.Name ELSE NULL END, CASE @sortExpr WHEN 'description' THEN c.Description ELSE NULL END, CASE @sortExpr WHEN 'relatedprojects' THEN c.RelatedProjects ELSE NULL ENDGO
What I want to do is further specify the sort such that I can define the direction. I tried:snippet from ORDER BY clause: CASE WHEN (@sortExpr = 'name' AND @SortAsc = 1) THEN c.Name ASC ELSE NULL END, CASE WHEN (@sortExpr = 'name' AND @SortAsc = 0) THEN c.Name DESC ELSE NULL END
but I get invalid syntax near ASC. Why? What is the correct syntax to use in this case?Am I going to have to abandon bi-directional sorting?