Skill 1.2.4
Create stored procedures
Key Concepts
A procedure accepts input parameters, OUTPUT parameters that return values to the caller, and table-valued parameters (TVPs) declared with a user-defined table type and READONLY, which pass many rows in one call. It can return result sets and an integer status with RETURN. SET NOCOUNT ON suppresses row-count messages, and EXECUTE AS runs the procedure under another security context. When a procedure builds dynamic SQL, sp_executesql with parameters prevents SQL injection, unlike string concatenation with EXEC.
Plans are compiled for the first parameter values used (parameter sniffing), which can be poor for very different later values. Options include OPTION (RECOMPILE), OPTIMIZE FOR, Query Store hints and, in compatibility level 160 and later, parameter sensitive plan optimization, which keeps several plans for one query. Granting EXECUTE on a procedure lets users change data only through it, because ownership chaining skips permission checks on tables with the same owner.