I have a simple sproc, what is the best way to code it for best query caching/optimization?
I currently have it as follows.
ALTER PROCEDURE dbo.OccupierGet
(
@OccupierID int = 0
)
AS
/* SET NOCOUNT ON */
--Get all details that can be editted.
select TOP 1 ID,AccountNumber,FirstName,LastName,Company,Telephone,Notes,
OccupierTypeID,Address,Address2,City,Country,Telephone2,HomePhone,CellPhone,WorkPhone,Fax,EmailAddress
from dbo.Occupier
where ID = @OccupierID
RETURN
Would it be better to build the sql query as a string and run with sp_executesql and pass the parameter? I'm asking because of query caching and the parameter I'm using.
Thank you! Tim
sp_executesqlin this situation. As long as you use parametrized queries to avoid SQL injection (and that's what you're already doing!), you're fine - just use the "plain" SQL inside your stored proc.TOPwithoutORDER BY. Also why isSET NOCOUNT ONcommented out?