Professional Documents
Culture Documents
Create Stored Procedures in The NorthWind
Create Stored Procedures in The NorthWind
Below are 20 questions for creating stored procedures in a NorthWind database using SQL Server,
along with answers and commands for executing stored procedures.
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:
Execution:
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:
4. Q: How do you create a stored procedure to get the details of a specific customer from the
Customers table? A:
Execution:
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:
6. Q: How do you create a stored procedure to insert a new order into the Orders table? A:
Execution:
7. Q: How do you create a stored procedure to update the ContactName of a customer in the
Customers table? A:
Execution:
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:
9. Q: How do you create a stored procedure to retrieve all orders for a specific customer from
the Orders table? A:
10. Q: How do you create a stored procedure to update the ShipAddress of an order in the
Orders table? A:
Execution:
11. Q: How do you create a stored procedure to insert a new product into the Products table? A:
Execution:
12. Q: How do you create a stored procedure to retrieve all customers from a specific country
from the Customers table? A:
Execution:
Execution:
14. Q: How do you create a stored procedure to delete a product from the Products table? A:
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:
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:
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:
Execution:
18. Q: How do you create a stored procedure to insert a new employee into the Employees table?
A:
Execution:
19. Q: How do you create a stored procedure to retrieve all employees who report to a specific
manager from the Employees table? A:
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:
These questions and answers provide a comprehensive guide to creating and using stored procedures in the
NorthWind database using SQL Server.