I have a bunch of stored procedures that I'm looking to optimize and want to know if changing from having the variables injected into the WHERE condition to having them inject into a variable declaration will help
The current statements look like this where the ids are passed into the stored procedure and then injected into the EXEC
DECLARE @EmployeeId int = 123
DECLARE @CustomerId int = 456
EXEC('
SELECT * FROM Employees WHERE Id = ' + @EmployeeId + '
SELECT * FROM Customers WHERE Id = ' + @CustomerId + '
')
Would there be any benefit of changing to something like this where the injected values are done into a variable?
DECLARE @EmployeeId int = 123
DECLARE @CustomerId int = 456
EXEC('
DECLARE @EmployeeId int = ' + @EmployeeId + '
DECLARE @CustomerId int = ' + @CustomerId + '
SELECT * FROM Employees WHERE Id = @EmployeeId
SELECT * FROM Customers WHERE Id = @CustomerId
')