Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

STORED PROCEDURE EXAMPLES

Below are 20 questions for creating stored procedures in a NorthWind database using SQL Server,
along with answers and commands for executing stored procedures.

Questions and Answers

1. Q: How do you create a stored procedure to insert a new customer into the Customers table?
A:

sql
Sao chép mã
CREATE PROCEDURE spInsertCustomer
@CustomerID NVARCHAR(5),
@CompanyName NVARCHAR(40),
@ContactName NVARCHAR(30),
@ContactTitle NVARCHAR(30),
@Address NVARCHAR(60),
@City NVARCHAR(15),
@Region NVARCHAR(15),
@PostalCode NVARCHAR(10),
@Country NVARCHAR(15),
@Phone NVARCHAR(24),
@Fax NVARCHAR(24)
AS
BEGIN
INSERT INTO Customers (CustomerID, CompanyName, ContactName, ContactTitle,
Address, City, Region, PostalCode, Country, Phone, Fax)
VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address,
@City, @Region, @PostalCode, @Country, @Phone, @Fax)
END

Execution:

EXEC spInsertCustomer 'ABCDE', 'New Company', 'John Doe', 'Manager', '123 Main
St', 'Anytown', 'CA', '12345', 'USA', '123-456-7890', '123-456-7891';

2. Q: How do you create a stored procedure to update the UnitPrice of a product in the
Products table? A:

CREATE PROCEDURE spUpdateProductPrice


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

Execution:

EXEC spUpdateProductPrice 1, 25.50;

3. Q: How do you create a stored procedure to delete an order from the Orders table? A:

sql
Sao chép mã
CREATE PROCEDURE spDeleteOrder
@OrderID INT
AS
BEGIN
DELETE FROM Orders
WHERE OrderID = @OrderID
END

Execution:

EXEC spDeleteOrder 10248;

4. Q: How do you create a stored procedure to get the details of a specific customer from the
Customers table? A:

CREATE PROCEDURE spGetCustomerDetails


@CustomerID NVARCHAR(5)
AS
BEGIN
SELECT * FROM Customers
WHERE CustomerID = @CustomerID
END

Execution:

EXEC spGetCustomerDetails 'ALFKI';

5. Q: How do you create a stored procedure to retrieve all products within a specific price range
from the Products table? A:

sql
Sao chép mã
CREATE PROCEDURE spGetProductsByPriceRange
@MinPrice DECIMAL(10, 2),
@MaxPrice DECIMAL(10, 2)
AS
BEGIN
SELECT * FROM Products
WHERE UnitPrice BETWEEN @MinPrice AND @MaxPrice
END

Execution:

EXEC spGetProductsByPriceRange 10.00, 50.00;

6. Q: How do you create a stored procedure to insert a new order into the Orders table? A:

CREATE PROCEDURE spInsertOrder


@CustomerID NVARCHAR(5),
@EmployeeID INT,
@OrderDate DATETIME,
@RequiredDate DATETIME,
@ShippedDate DATETIME,
@ShipVia INT,
@Freight DECIMAL(10, 2),
@ShipName NVARCHAR(40),
@ShipAddress NVARCHAR(60),
@ShipCity NVARCHAR(15),
@ShipRegion NVARCHAR(15),
@ShipPostalCode NVARCHAR(10),
@ShipCountry NVARCHAR(15)
AS
BEGIN
INSERT INTO Orders (CustomerID, EmployeeID, OrderDate, RequiredDate,
ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion,
ShipPostalCode, ShipCountry)
VALUES (@CustomerID, @EmployeeID, @OrderDate, @RequiredDate, @ShippedDate,
@ShipVia, @Freight, @ShipName, @ShipAddress, @ShipCity, @ShipRegion,
@ShipPostalCode, @ShipCountry)
END

Execution:

EXEC spInsertOrder 'VINET', 5, '2023-06-20', '2023-07-01', '2023-06-22', 3,


32.38, 'Vins et alcools Chevalier', '59 rue de l\'Abbaye', 'Reims', NULL,
'51100', 'France';

7. Q: How do you create a stored procedure to update the ContactName of a customer in the
Customers table? A:

CREATE PROCEDURE spUpdateCustomerContact


@CustomerID NVARCHAR(5),
@NewContactName NVARCHAR(30)
AS
BEGIN
UPDATE Customers
SET ContactName = @NewContactName
WHERE CustomerID = @CustomerID
END

Execution:

EXEC spUpdateCustomerContact 'ALFKI', 'Maria Anders';

8. Q: How do you create a stored procedure to delete a customer from the Customers table? A:

Sao chép mã
CREATE PROCEDURE spDeleteCustomer
@CustomerID NVARCHAR(5)
AS
BEGIN
DELETE FROM Customers
WHERE CustomerID = @CustomerID
END

Execution:

EXEC spDeleteCustomer 'ALFKI';

9. Q: How do you create a stored procedure to retrieve all orders for a specific customer from
the Orders table? A:

CREATE PROCEDURE spGetOrdersByCustomer


@CustomerID NVARCHAR(5)
AS
BEGIN
SELECT * FROM Orders
WHERE CustomerID = @CustomerID
END
Execution:

EXEC spGetOrdersByCustomer 'ALFKI';

10. Q: How do you create a stored procedure to update the ShipAddress of an order in the
Orders table? A:

CREATE PROCEDURE spUpdateOrderShipAddress


@OrderID INT,
@NewShipAddress NVARCHAR(60)
AS
BEGIN
UPDATE Orders
SET ShipAddress = @NewShipAddress
WHERE OrderID = @OrderID
END

Execution:

EXEC spUpdateOrderShipAddress 10248, '123 New Address';

11. Q: How do you create a stored procedure to insert a new product into the Products table? A:

CREATE PROCEDURE spInsertProduct


@ProductName NVARCHAR(40),
@SupplierID INT,
@CategoryID INT,
@QuantityPerUnit NVARCHAR(20),
@UnitPrice DECIMAL(10, 2),
@UnitsInStock SMALLINT,
@UnitsOnOrder SMALLINT,
@ReorderLevel SMALLINT,
@Discontinued BIT
AS
BEGIN
INSERT INTO Products (ProductName, SupplierID, CategoryID, QuantityPerUnit,
UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued)
VALUES (@ProductName, @SupplierID, @CategoryID, @QuantityPerUnit,
@UnitPrice, @UnitsInStock, @UnitsOnOrder, @ReorderLevel, @Discontinued)
END

Execution:

EXEC spInsertProduct 'New Product', 1, 1, '10 boxes', 20.00, 100, 0, 10, 0;

12. Q: How do you create a stored procedure to retrieve all customers from a specific country
from the Customers table? A:

CREATE PROCEDURE spGetCustomersByCountry


@Country NVARCHAR(15)
AS
BEGIN
SELECT * FROM Customers
WHERE Country = @Country
END

Execution:

EXEC spGetCustomersByCountry 'Germany';


13. Q: How do you create a stored procedure to update the Freight of an order in the Orders
table? A:

CREATE PROCEDURE spUpdateOrderFreight


@OrderID INT,
@NewFreight DECIMAL(10, 2)
AS
BEGIN
UPDATE Orders
SET Freight = @NewFreight
WHERE OrderID = @OrderID
END

Execution:

EXEC spUpdateOrderFreight 10248, 50.00;

14. Q: How do you create a stored procedure to delete a product from the Products table? A:

CREATE PROCEDURE spDeleteProduct


@ProductID INT
AS
BEGIN
DELETE FROM Products
WHERE ProductID = @ProductID
END

Execution:

EXEC spDeleteProduct 1;

15. Q: How do you create a stored procedure to retrieve all orders shipped by a specific shipper
from the Orders table? A:

CREATE PROCEDURE spGetOrdersByShipper


@ShipperID INT
AS
BEGIN
SELECT * FROM Orders
WHERE ShipVia = @ShipperID
END

Execution:

EXEC spGetOrdersByShipper 3;

16. Q: How do you create a stored procedure to update the Discontinued status of a product in
the Products table? A:

CREATE PROCEDURE spUpdateProductDiscontinued


@ProductID INT,
@Discontinued BIT
AS
BEGIN
UPDATE Products
SET Discontinued = @Discontinued
WHERE ProductID = @ProductID
END
Execution:

EXEC spUpdateProductDiscontinued 1, 1;

17. Q: How do you create a stored procedure to retrieve the details of a specific order from the
Orders table? A:

CREATE PROCEDURE spGetOrderDetails


@OrderID INT
AS
BEGIN
SELECT * FROM Orders
WHERE OrderID = @OrderID
END

Execution:

EXEC spGetOrderDetails 10248;

18. Q: How do you create a stored procedure to insert a new employee into the Employees table?
A:

CREATE PROCEDURE spInsertEmployee


@LastName NVARCHAR(20),
@FirstName NVARCHAR(10),
@Title NVARCHAR(30),
@TitleOfCourtesy NVARCHAR(25),
@BirthDate DATETIME,
@HireDate DATETIME,
@Address NVARCHAR(60),
@City NVARCHAR(15),
@Region NVARCHAR(15),
@PostalCode NVARCHAR(10),
@Country NVARCHAR(15),
@HomePhone NVARCHAR(24),
@Extension NVARCHAR(4),
@Photo VARBINARY(MAX),
@Notes NTEXT,
@ReportsTo INT,
@PhotoPath NVARCHAR(255)
AS
BEGIN
INSERT INTO Employees (LastName, FirstName, Title, TitleOfCourtesy,
BirthDate, HireDate, Address, City, Region, PostalCode, Country, HomePhone,
Extension, Photo, Notes, ReportsTo, PhotoPath)
VALUES (@LastName, @FirstName, @Title, @TitleOfCourtesy, @BirthDate,
@HireDate, @Address, @City, @Region, @PostalCode, @Country, @HomePhone,
@Extension, @Photo, @Notes, @ReportsTo, @PhotoPath)
END

Execution:

EXEC spInsertEmployee 'Doe', 'John', 'Sales Manager', 'Mr.', '1980-01-01',


'2023-06-20', '123 Main St', 'Anytown', 'CA', '12345', 'USA', '123-456-7890',
'1234', NULL, 'Notes', 2, 'path/to/photo.jpg';

19. Q: How do you create a stored procedure to retrieve all employees who report to a specific
manager from the Employees table? A:

CREATE PROCEDURE spGetEmployeesByManager


@ManagerID INT
AS
BEGIN
SELECT * FROM Employees
WHERE ReportsTo = @ManagerID
END

Execution:

EXEC spGetEmployeesByManager 2;

20. Q: How do you create a stored procedure to update the Title of an employee in the
Employees table? A:

sql
Sao chép mã
CREATE PROCEDURE spUpdateEmployeeTitle
@EmployeeID INT,
@NewTitle NVARCHAR(30)
AS
BEGIN
UPDATE Employees
SET Title = @NewTitle
WHERE EmployeeID = @EmployeeID
END

Execution:

EXEC spUpdateEmployeeTitle 1, 'Senior Sales Manager';

These questions and answers provide a comprehensive guide to creating and using stored procedures in the
NorthWind database using SQL Server.

You might also like