exec ('select * from variable_table_name
order by @variable')
I’m trying to put together a dynamic sql statment where you specify a table name and sort column. It would be nice if I can just specify a table name to make the sql work.
Is there a way to choose the first column of a table to sort by if @variable sort by not specified?
You can use:
From ORDER BY Clause (Transact-SQL):
(emphasis mine)