This post describes how to create a stored procedure in MSSQL that takes parameters. For this we will use the simple table below:
CREATE TABLE SomeTable(
Id INT IDENTITY(1,1) PRIMARY KEY (ID) NOT NULL,
SomeText VARCHAR(MAX) NOT NULL,
)
Using a CREATE PROCEDURE
statement we can create a stored procedure:
GO
CREATE PROCEDURE GetFromSomeTableById
(
@Id INT -- separate more parameters by comma
)
AS
BEGIN
SET NOCOUNT ON
SELECT * FROM SomeTable WHERE Id = @Id;
END
The above created a stored procedure called GetFromSomeTableById
, it has a single parameter called @Id
which has the type int
. This is then used to SELECT from the previously created table. The procedure can be invoked using the EXEC
keyword:
EXEC GetFromSomeTableById @Id = 1
That is all there is to it, I hope you found this short post helpful!