Understand how to create a user-defined stored procedure.
Creating Stored Procedures in SQL-Server
You can use Enterprise Manager in SQL Server 2019 to create a user-defined stored procedure. Here are the steps on how to do it:
Open Enterprise Manager and connect to your SQL Server instance.
Expand the instance, then expand Databases, and then expand the database that you want to create the stored procedure in.
Expand Programmability, and then right-click Stored Procedures and select New Stored Procedure.
In the New Stored Procedure dialog box, enter a name for the stored procedure, and then select the database that you want to create the stored procedure in.
In the Query pane, enter the Transact-SQL code for the stored procedure.
Click the Parse button to check the syntax of the stored procedure.
If there are no errors, click the Execute button to create the stored procedure.
The stored procedure will be created in the database and you will be able to see it in the Stored Procedures folder in Object Explorer.
Here is an example of a stored procedure that you can create:
CREATE PROCEDURE uspGetEmployees
AS
BEGIN
SELECT * FROM Employees
END
This stored procedure will return all of the employees from the Employees table. To execute the stored procedure, you can use the following Transact-SQL code:
EXEC uspGetEmployees
This will return all of the employees from the Employees table.
You can use either Enterprise Manager or Transact-SQL to create a stored procedure.
Using Enterprise Manager
To create a stored procedure with Enterprise Manager:
Drill down to the Stored Procedures folder.
Right-click the Stored Procedures folder.
Choose the New Stored Procedure menu option.
Enter the appropriate Transact-SQL statements in the Stored Procedure Properties dialog box, shown below:
As with triggers, you will need to use Transact-SQL within Enterprise Manager to create and modify stored procedures.
Using Transact-SQL
To create a stored procedure using Transact-SQL, you will use the CREATE PROCEDURE statement, which uses the syntax shown in the following series of images
In the next lesson, you will practice creating a user-defined stored procedure.