SQL Procedures

Stored procedures are pre-prepared SQL queries that we can save in the program and use them multiple times. It is beneficial to create such procedures for routine tasks. When we want to use them, we can simply run them without rewriting the code. It is also possible to pass parameters to a procedure, which will evaluate them accordingly in the executed procedure.

Format:

CREATE PROCEDURE procedure_name
AS
SQL_Query
GO;

Executing a procedure:

EXEC procedure_name;

Using parameters:

CREATE PROCEDURE SelectEmployees @Company nvarchar(30), @City nvarchar(30)
AS
SELECT * FROM Employees WHERE Company = @Company AND City = @City
GO;

EXEC SelectEmployees @Company = 'Nike', @City = 'Warsaw';

Interesting article? Share further!

Leave A Comment