CREATE TABLE [TestTable](
[ID] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Size] [int] NULL,
[Created] [datetime] NULL
)
Сортувати її за значення певного параметра можна так:
declare @order nvarchar(50)
set @order = 'Created DESC'
select *
from TestTable
order by
case @order
when 'ID' then cast(ID as sql_variant)
when 'Name' then cast(Name as sql_variant)
when 'Size' then cast(Size as sql_variant)
when 'Created' then cast(Created as sql_variant)
END,
case @order
when 'ID DESC' then cast(ID as sql_variant)
when 'Name DESC' then cast(Name as sql_variant)
when 'Size DESC' then cast(Size as sql_variant)
when 'Created DESC' then cast(Created as sql_variant)
END desc
Немає коментарів:
Дописати коментар