エイリアスに基づいてセレクションを注文しようとしていますが、方法がわかりません。次に例を示します。
select distinct top 100 id,
col1,
col2,
CASE WHEN @orderFormat = 'this' then col1
WHEN @orderFormat = 'that' then col2
END as orderby
from table
where col1 = like '%'
order by Len(orderby) asc, orderby asc
エイリアス「orderby」を引数として渡すと、無効な列として報告されます。
私の目標は、可変列を英数字順に並べられるようにすることです。'order by Len(orderby) asc、orderby asc は機能しますが、エイリアスでは機能しないことを知っています。
誰かがこれを回避する良い方法を知っていますか、それとも私が何か間違ったことをしているのですか?
ありがとう!
編集:
私はこれに選択機能を取り除くことができました:
select top 200 Clip_Name as orderby
from Clips
order by Len(orderby) asc, orderby asc
Clip_Name は として宣言されcolumn Clip_Name(nvarchar, not null)
ます。Microsoft SQL Server 2008 R2 Edition のエラーは ですMsg 207, Level 16, State 1, Line 1
Invalid column name 'orderby'
。
ただし、これは機能します (エイリアスなし):
select top 200 Clip_Name
from Clips
order by len(FLE_ID) desc, FLE_ID desc