Call a reusable module with a bound parameter
Define a session local stored procedure and pass a typed input value by name.
What you are trying to do
A service repeatedly needs work items above a points threshold. A temporary procedure packages the read query and accepts the threshold as an input.
Work through it
- Read GO as a client batch separator. CREATE PROCEDURE starts its own batch.
- Trace @MinimumPoints from the procedure signature to the WHERE predicate.
- Read the named argument at EXEC and compare its value with the synthetic rows.
- DROP PROCEDURE removes the session local procedure. If the client stops early, closing the session removes it too.
- Run only in a SQL Server session that supports GO. The procedure name starts with # and is temporary.
Check your work
The procedure call returns exactly two rows, task 101 with 8 points and task 103 with 12 points. Task 102 with 4 points is filtered out. The temporary procedure is dropped at the end.
Practice SQL
Open in SQL FlowCREATE PROCEDURE #ListWorkItems
@MinimumPoints int
AS
BEGIN
SET NOCOUNT ON;
SELECT TaskId, Title, Points
FROM (VALUES (101, N'Cache review', 8), (102, N'Queue alert', 4), (103, N'Usage report', 12)) AS WorkItem(TaskId, Title, Points)
WHERE Points >= @MinimumPoints
ORDER BY TaskId;
END;
GO
EXEC #ListWorkItems @MinimumPoints = 7;
DROP PROCEDURE #ListWorkItems;
Knowledge check
Commands in this lesson
Next lesson Bind values in dynamic SQL