Sometimes we need to pass the variable to our SQL Block in Stored Proc or User Defined Function.
e.g. in code section of dotnetfunda
Title Ascending or Title Decending
Views Ascending or Views Decending
any other fields.
So the following code is handy in such cases.
The following coode is self-explanatory but still any doubt, please let me know.
declare @sortorder1 varchar(200)Note:
set @sortorder1 = 'CustID DESC'
declare @sortorder2 varchar(200)
set @sortorder2 = 'CntID DESC'
CASE WHEN @sortorder1 = 'CustID ASC' THEN CustomerID END DESC,
CASE WHEN @sortorder1 = 'CustID DESC' THEN CustomerID END DESC,
CASE WHEN @sortorder2 = 'CntID ASC' THEN CountryID END ASC,
CASE WHEN @sortorder2 = 'CntID DESC' THEN CountryID END DESC
One drawback of this code is that we have only fixed orderby clauses, in case a new orderby clause is required to be added;
we need to modify the above code for that value.