Это будет делать, что вы хотите при условии, что типы данных совместимы
order by
case @pcsort
when '' then compcode
else received
end ASC,
compcode ASC
В более общем случае вам нужен один CASE на столбец сортировки при условии, что типы данных совместимы
order by
case @pcsort
when '' then compcode
else received
end ASC,
case @pcsort
--safe to sort on same column agaon , or use a constant
when '' then compcode or <constant of same type as compcode>
else compcode
end ASC
Если типы данных несовместимы, вам нужно больше дел и много констант
order by
case @pcsort
when '' then compcode
else <constant of same type as compcode>
end ASC,
case @pcsort
when '' then <constant of same type as received>
else received
end ASC,
case @pcsort
when '' then <constant of same type as compcode>
else compcode
end ASC