is there any way that you can call sp_executesql with parameters that don't depend in the order they are defined in the store? the same query with exec works well, and if you have the same order it also works well, but it's a pain having to match the parameter one by one, because sometime I am generatin the call dynamically with helpers, and if the dto object don't have the same fields in the same order, doesn't work well.
create procedure ordertest @PARAM1 INT, @PARAM2 INT AS BEGIN SELECT @PARAM1 AS ONE, @PARAM2 AS TWO END -- this works EXEC ordertest @PARAM1 = 1, @PARAM2 = 2 exec sp_executesql N'exec ordertest @PARAM1, @PARAM2', N'@param1 int, @param2 int', @param2 = '2', @param1 = '1' EXEC ordertest @PARAM2 = 2, @PARAM1 = 1 -- this doesn't work exec sp_executesql N'exec ordertest @PARAM2, @PARAM1', N'@param1 int, @param2 int', @param2 = '2', @param1 = '1'
Sure you can do this. You just need to add which parameter is which when you call it.
exec sp_executesql N'exec ordertest @PARAM2 = @Param2, @PARAM1 = @Param1', N'@param1 int, @param2 int', @param2 = '2', @param1 = '1'