Hi i have a store procedure, where i do a select query. I want order this by an external parameter.
I post an minimal example:
CREATE PROCEDURE [dbo]
Another option is to use an expression for the column that you want to sort by.
DECLARE @OrderBy INT SET @OrderBy = 4 SELECT * FROM MySourceTable ORDER BY COL_NAME(OBJECT_ID('MySourceTable'), @OrderBy )
It is usually better to avoid dynamic sql if you can.