In this exercise you will create a stored procedure that contains a transaction.
Before developing the sProc, you must modify your Northwind database.
- The Save-a-lot Markets in Boise is being hit hard by inflation, so they decided to change their name to Save-a-Bit Markets.
The transaction should update the information for CompanyName in the customers table.
- Pass BOTH the current company name and the new company name to the sProc as parameters.
- Declare a local variable called cID.
- The first query in the transaction should be a SELECT statement that returns the CustomerID for the current company name and assigns that CustomerID to cID. You will need parentheses around the SELECT query on the right side of the SET statement.
- The next query should update the record for that CustomerID, changing the current company name to the new company name.
- Be sure to save the transaction as a stored procedure and test it.
Solution:
delimiter $$
DROP PROCEDURE IF EXISTS sp_ModifyName$$
CREATE PROCEDURE sp_ModifyName (IN currentName VARCHAR(40), IN newName VARCHAR(40))
BEGIN
-- Declare a local variable called cID.
DECLARE cID VARCHAR(5);
-- Include a SELECT statement that returns the CustomerID for the current company name and assigns that CustomerID to cID.
SET cID = (SELECT CustomerID from customers where CompanyName = currentName);
-- Update the record for that CustomerID, changing the current company name to the new company name.
UPDATE customers SET CompanyName = newName WHERE CustomerID = cID;
END$$
delimiter ;
Testing:
Test the sProc with a statement like the following:
CALL sp_ModifyName ('Save-a-lot Markets', 'Save-a-Bit Markets');
Verify that the company name changed with the following query:
SELECT CompanyName from customers WHERE CustomerID = "SAVEA";
In this exercise you will create a stored procedure that contains a transaction for the Northwind database.
Design a transaction to handle the following situation: the owner of Northwind has decided that she no longer wishes to do business with Supplier 18, Aux Joyeux Ecclésiastiques, because they have a funny name. Instead, she wishes to replace them with Wee Willie's Exotic (Cooking) Oils.
The information for Wee Willie's follows:
supplierID | |
CompanyName |
Wee Willie's Exotic (Cooking) Oils |
ContactName |
Willie Brown |
ContactTitle |
Owner/Sales Manager |
Address |
1412 Post Dr. |
City | Inkom |
Region | ID |
PostalCode | 83245 |
Country | USA |
Phone | (208) 775-4462 |
Fax | (208) 775-4463 |
HomePage |
The transaction should shift business from one supplier to another.
- Add the information for Wee Willie's to the Supplier table.
- Find the SupplierID assigned to Wee Willie and assign it to a local variable. This link may help.
- Update the Products table, replacing all references to SupplierID 18 with Wee Willie's SupplierID.
- Remove SupplierID 18 from the Supplier table.
- Save the transaction as a stored procedure.
Solution:
DELIMITER $$
CREATE PROCEDURE sp_ChangeSupplier()
BEGIN
DECLARE nextID INT;
SET foreign_key_checks = 0;
-- Add the information for Wee Willie's to the Supplier table.
INSERT INTO suppliers(SupplierID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax, HomePage)
VALUES (NULL, "Wee Willie‘s Exotic (Cooking) Oils", "Willie Brown", "Owner/Sales Manager", "1412 Post Dr.", "Inkom", "ID", "83245", "USA", "(208) 775-4462", "(208) 775-4463", NULL);
-- Find the SupplierID assigned to Wee Willie and assign it to a local variable.
SET nextID = LAST_INSERT_ID();
-- Update the Products table, replacing all references to SupplierID 18 with Wee Willie's SupplierID.
UPDATE products SET SupplierID = nextID WHERE SupplierID = 18;
-- Remove SupplierID 18 from the Supplier table.
DELETE FROM suppliers WHERE SupplierID = 18;
END$$
DELIMITER ;
Testing:
Test to verify that the new supplier has been entered in the suppliers table with the following nested query:
SELECT SupplierID, CompanyName FROM suppliers WHERE SupplierID = (SELECT MAX(SupplierID) FROM suppliers);
Test to verify that the new supplier has been assigned to all of Supplier 18, Aux Joyeux Ecclésiastiques accounts in the products table with the following nested query:
SELECT ProductID, ProductName from products WHERE SupplierID = (SELECT MAX(SupplierID) FROM products);