...@Sort bit)
AS
SELECT
..............
Run Code Online (Sandbox Code Playgroud)
我想Order只在Sort = true时才这样做
我怎么才能意识到这一点?
谢谢.
我不认为这个工作我必须承认,但确实如此.在这个例子中,如果@sort设置为true,我们按城镇排序,否则,我们使用其他一些默认排序(必须与排序列相同).
DECLARE @sort bit
SET @sort = 0
SELECT [addressId]
,[customerId]
,[addressTypeId]
,[address1]
,[address2]
,[address3]
,[town]
,[county]
,[postcode]
,[countryCode]
FROM
[dbo].[tblAddress]
ORDER BY
CASE
WHEN @sort = 1 THEN town
ELSE 'A'
END ASC
Run Code Online (Sandbox Code Playgroud)
根据评论建议编辑.
...
ORDER BY
CASE
WHEN @sort = 1 THEN sortcolumn
ELSE 1 --constant value with same type of "sortcolumn" eg '19000101' or 'a'
END
Run Code Online (Sandbox Code Playgroud)