Consider this sample table...create table #orderbytest (a char(1), b int)insert #orderbytestselect char((n%26) + 97), floor(rand(cast(newid() as varbinary(4))) * 1000000)from numbers -- (FYI - create table numbers (n int) and fill with 1 thru 10000)go
I am trying to do a Dynamic ORDER BY clause like this ...declare @orderby char(1)set @orderby = 'b'select a, bfrom #orderbytestorder by case when @orderby = 'a' then a when @orderby = 'b' then b end ASC
Now when I set @orderby = 'b' it works, but if I set @orderby = 'a' I get ...quote:
Server: Msg 245, Level 16, State 1, Line 4Syntax error converting the varchar value 'a' to a column of data type int.