Stored Procedures
Create and call a stored procedure
Section titled “Create and call a stored procedure”Stored procedures can be created through a database management GUI (SQL Server example), or through a SQL statement as follows:
-- Define a name and parametersCREATE PROCEDURE Northwind.getEmployee @LastName nvarchar(50), @FirstName nvarchar(50)AS
-- Define the query to be runSELECT FirstName, LastName, DepartmentFROM Northwind.vEmployeeDepartmentWHERE FirstName = @FirstName AND LastName = @LastNameAND EndDate IS NULL;Calling the procedure:
EXECUTE Northwind.getEmployee N'Ackerman', N'Pilar';
-- OrEXEC Northwind.getEmployee @LastName = N'Ackerman', @FirstName = N'Pilar';GO
-- OrEXECUTE Northwind.getEmployee @FirstName = N'Pilar', @LastName = N'Ackerman';GORemarks
Section titled “Remarks”Stored Procedures are SQL statements stored in the database that can be executed or called in queries. Using a stored procedure allows encapsulation of complicated or frequently used logic, and improves query performance by utilizing cached query plans. They can return any value a standard query can return.
Other benefits over dynamic SQL expressions are listed on Wikipeida.