Welcome Back

Google icon Sign in with Google
OR
I agree to abide by Pharmadaily Terms of Service and its Privacy Policy

Create Account

Google icon Sign up with Google
OR
By signing up, you agree to our Terms of Service and Privacy Policy
Instagram
youtube
Facebook

Inline Table-Valued Function for Orders in a Date Range

Inline Table-Valued Function for Orders in a Date Range

orders table:

Query Explanation:

  1. Procedure Definition: We create a procedure GetOrdersByDateRange that accepts StartDate and EndDate as parameters.

  2. SELECT Query: Inside the procedure, we select orders from the Orders table where the OrderDate is between the given start and end dates.

  3. Calling Procedure: We use the CALL statement to execute the procedure with the date range you want.

 

SQL Query:

USE SalesInventoryDB;

-- Creating a stored procedure to fetch orders in a date range
DELIMITER //

CREATE PROCEDURE GetOrdersByDateRange(
    IN StartDate DATE,
    IN EndDate DATE
)
BEGIN
    SELECT * 
    FROM Orders
    WHERE OrderDate BETWEEN StartDate AND EndDate;
END //

DELIMITER ;



-- Once the procedure is created, you can call it like this:
CALL GetOrdersByDateRange('2023-01-01', '2024-12-31');

 

Output: