I want to Order a SQL Select Query where there\'s 2 fields that are in the order by. I then need to decide if one is Descending and the other as Ascending. How is this done<
You will have to create your SQL statement dynamically in order to use a variable:
DECLARE @asc_desc VARCHAR(4);
SET @asc_desc = 'DESC';
DECLARE @sql NVARCHAR(1000);
SET @sql = 'Select * from Customer Order By Date ' + @asc_desc + ', Name';
EXEC sp_executesql @sql
This will order Date DESCENDING
and Name ASCENDING
.
You only need to add DESC
if you want to use DESCENDING
as ASCENDING
is default.