In my stored procedure I want to make the order by clause dynamic.
Select * from JOBDETAILS where JobOrOpportunity='O'
and(@OppCreatedBy IS NULL OR OppCreatedBy = @OppCreatedBy or PrivateOrPublic='PB')
and (@CustomerID IS NULL OR CustomerID = @CustomerID)
and (@JobFromDt IS NULL OR JOBDETAILS.JobDate >=@JobFromDt)
and (@JobToDt IS NULL OR JOBDETAILS.JobDate <=@JobToDt)
and (JOBDETAILS.JobStatus <>'LS')
order by @SortExpression
But I am getting compilation error-
"The SELECT item identified by the ORDER BY number 1 contains a variable as part of the expression identifying a column position. Variables are only allowed when ordering by an expression referencing a column name."