No. | Title |
---|---|
1 | Explanation |
2 | Coding Example |
3 | Case Studies |
4 | Conclusion |
CREATE PROCEDURE InsertCustomer
@CustomerName VARCHAR(100),
@Email VARCHAR(100),
@PhoneNumber VARCHAR(20)
AS
BEGIN
INSERT INTO Customers (CustomerName, Email, PhoneNumber)
VALUES (@CustomerName, @Email, @PhoneNumber);
END
EXEC InsertCustomer @CustomerName = 'John Doe', @Email = 'john.doe@example.com', @PhoneNumber = '123-456-7890';
CREATE PROCEDURE ProcessOrder
@OrderID INT
AS
BEGIN
-- Calculate total order value
DECLARE @TotalOrderValue DECIMAL(10, 2);
SELECT @TotalOrderValue = SUM(Quantity * Price)
FROM OrderItems
WHERE OrderID = @OrderID;
-- Update inventory levels
UPDATE Products
SET StockQuantity = StockQuantity - (SELECT SUM(Quantity) FROM OrderItems WHERE OrderID = @OrderID)
WHERE ProductID IN (SELECT ProductID FROM OrderItems WHERE OrderID = @OrderID);
-- Generate invoice
INSERT INTO Invoices (OrderID, TotalAmount)
VALUES (@OrderID, @TotalOrderValue);
END
CREATE PROCEDURE GenerateSalesReport
@StartDate DATE,
@EndDate DATE
AS
BEGIN
-- Aggregating sales data
SELECT
YEAR(OrderDate) AS SalesYear,
MONTH(OrderDate) AS SalesMonth,
SUM(Quantity * Price) AS TotalSales
FROM
Orders
INNER JOIN
OrderItems ON Orders.OrderID = OrderItems.OrderID
WHERE
OrderDate >= @StartDate AND OrderDate <= @EndDate
GROUP BY
YEAR(OrderDate), MONTH(OrderDate);
END
CREATE PROCEDURE InsertNewCustomer
@CustomerName VARCHAR(100),
@Email VARCHAR(100),
@Address VARCHAR(200)
AS
BEGIN
-- Validate and sanitize customer input
SET @CustomerName = REPLACE(@CustomerName, '<script>', '');
SET @Email = REPLACE(@Email, '<script>', '');
SET @Address = REPLACE(@Address, '<script>', '');
-- Insert validated data into the Customers table
INSERT INTO Customers (CustomerName, Email, Address)
VALUES (@CustomerName, @Email, @Address);
END
CREATE PROCEDURE TransformLegacyData
AS
BEGIN
-- Extract data from the legacy system
SELECT
LegacyCustomerID,
LegacyCustomerName,
LegacyEmail
INTO
#TempLegacyData
FROM
LegacyCustomers;
-- Transform and load data into the new database schema
INSERT INTO Customers (CustomerID, CustomerName, Email)
SELECT
LegacyCustomerID,
LegacyCustomerName,
LegacyEmail
FROM
#TempLegacyData;
-- Clean up temporary table
DROP TABLE #TempLegacyData;
END
CREATE PROCEDURE UpdateOrderStatusBatch
@OrderIDs TABLE (OrderID INT)
AS
BEGIN
-- Update the status of multiple orders
UPDATE Orders
SET Status = 'Completed'
WHERE OrderID IN (SELECT OrderID FROM @OrderIDs);
END