0% found this document useful (0 votes)
29 views

7+IM+Lab+Stored+Procedures+and+Functions

Uploaded by

davethegreat343
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

7+IM+Lab+Stored+Procedures+and+Functions

Uploaded by

davethegreat343
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB 7: STORED PROCEDURES AND FUNCTIONS

Objectives:
• Understand the concept of stored procedures and functions.
• Learn how to create and use them for data manipulation.

Lab Activities:
1. Creating stored procedures to perform data manipulation tasks.
2. Writing user-defined functions for custom calculations.
3. Calling stored procedures and functions from queries.

7.1 - CREATING STORED PROCEDURES TO PERFORM DATA MANIPULATION TASKS

Creating stored procedures in SQL Server Management Studio (SSMS) is a fundamental skill
for performing data manipulation tasks and automating database operations. Here are the steps
to create stored procedures using SSMS:

1. Connect to the Database:


 Open SQL Server Management Studio.
 Connect to the SQL Server instance where your database is located.
2. Open a New Query Window:
 In SSMS, click "New Query" to open a new query window.
3. Write the Stored Procedure:
 Use the CREATE PROCEDURE statement to define the stored procedure. The basic
syntax is as follows:

CREATE PROCEDURE procedure_name


AS
BEGIN
-- SQL statements to perform data manipulation tasks
END;
 Replace procedure_name with your desired name for the stored procedure.
 Write the SQL statements within the BEGIN and END block to perform the data
manipulation tasks you require. For example, you can use SELECT, INSERT, UPDATE,
DELETE, and other SQL statements.
 Here's an example of a simple stored procedure that selects data from a table:

CREATE PROCEDURE GetCustomers


AS
BEGIN
SELECT * FROM Customers;
END;

4. Execute the CREATE PROCEDURE Statement:


 Highlight the entire CREATE PROCEDURE statement in your query window.
 Click the "Execute" button in the SSMS toolbar or use the F5 key to execute the
statement.
 SSMS will create the stored procedure in the database.

5. Verify the Stored Procedure:


 To verify that the stored procedure was created successfully, expand the
"Programmability" folder in Object Explorer, and then expand the "Stored Procedures"
folder under your database. You should see your newly created stored procedure
listed there.

6. Executing the Stored Procedure:

 You can execute the stored procedure by calling it in a query window. For example:
EXEC GetCustomers;

7. Managing Stored Procedures:

 To modify an existing stored procedure, you can right-click it in Object Explorer and
select "Modify." Make your changes and execute the modified code.

 To delete a stored procedure, right-click it in Object Explorer and select "Delete."

8. Parameterized Stored Procedures:

 You can create stored procedures with parameters to make them more flexible and
reusable. Parameters allow you to pass values to the procedure when you execute it.
Here's an example:

CREATE PROCEDURE GetCustomerByID


@CustomerID INT
AS
BEGIN
SELECT * FROM Customers WHERE CustomerID = @CustomerID;
END;

 To execute a parameterized stored procedure, you provide the parameter value when
calling it:
EXEC GetCustomerByID @CustomerID = 123;

Creating stored procedures in SQL Server using SSMS allows you to encapsulate data
manipulation logic and provides a way to execute complex tasks with a single call. It's an
essential skill for database developers and administrators.

Exercise 1: Creating a Simple SELECT Procedure


Create a stored procedure that selects data from a table.
1. Open SQL Server Management Studio (SSMS) and connect to your database.
2. Open a new query window.
3. Write a stored procedure to select data from a table, for example:

CREATE PROCEDURE SelectEmployees


AS
BEGIN
SELECT * FROM Employees;
END;
4. Execute the CREATE PROCEDURE statement to create the stored procedure.
5. Verify that the procedure was created successfully by checking the "Stored
Procedures" folder in Object Explorer.
6. Execute the stored procedure by running the following command:
EXEC SelectEmployees;

Exercise 2: Creating a Parameterized Procedure


Create a stored procedure with parameters to retrieve specific data.
1. Open a new query window.
2. Write a stored procedure that takes a parameter to filter data, for example:

CREATE PROCEDURE GetEmployeeByID


@EmployeeID INT
AS
BEGIN
SELECT * FROM Employees WHERE EmployeeID = @EmployeeID;
END;

3. Execute the CREATE PROCEDURE statement to create the stored procedure.


4. Verify that the procedure was created successfully.
5. Execute the parameterized stored procedure by running the following command:
EXEC GetEmployeeByID @EmployeeID = 123;

Exercise 3: Creating an INSERT Procedure


Create a stored procedure to insert data into a table.
1. Open a new query window.
2. Write a stored procedure to insert data into a table, for example:

CREATE PROCEDURE InsertCustomer


@CustomerName NVARCHAR(255),
@ContactEmail NVARCHAR(255)
AS
BEGIN
INSERT INTO Customers (CustomerName, ContactEmail)
VALUES (@CustomerName, @ContactEmail);
END;

3. Execute the CREATE PROCEDURE statement to create the stored procedure.


4. Verify that the procedure was created successfully.
5. Execute the stored procedure with parameter values to insert data:
EXEC InsertCustomer @CustomerName = 'ABC Corp', @ContactEmail =
'abc@example.com';

Exercise 4: Creating an UPDATE Procedure


Create a stored procedure to update data in a table.
1. Open a new query window.
2. Write a stored procedure to update data in a table, for example:

CREATE PROCEDURE UpdateProductPrice


@ProductID INT,
@NewPrice DECIMAL(10, 2)
AS
BEGIN
UPDATE Products
SET Price = @NewPrice
WHERE ProductID = @ProductID;
END;

3. Execute the CREATE PROCEDURE statement to create the stored procedure.


4. Verify that the procedure was created successfully.
5. Execute the stored procedure with parameter values to update data:
EXEC UpdateProductPrice @ProductID = 456, @NewPrice = 19.99;

Exercise 5: Creating a DELETE Procedure


Create a stored procedure to delete data from a table.
1. Open a new query window.
2. Write a stored procedure to delete data from a table, for example:
3. Execute the CREATE PROCEDURE statement to create the stored procedure.
4. Verify that the procedure was created successfully.
5. Execute the stored procedure with parameter values to delete data:
EXEC DeleteEmployee @EmployeeID = 789;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy