Good morning everyone. I have a query that isn't behaving like I want. The query is to be used in an SSRS report. I want the query to return all records if nothing is selected for a start and end date. The way I have it now nothing shows up if the begin and start dates aren't supplied. Can someone please take a look at the below SP and show me how to alter it so that all records are returned if the start and end dates are omitted? Or maybe my approach is wrong and someone can point me in the right direction?
USE [mybookstore]
GO
/****** Object: StoredProcedure [dbo].[GetSales] Script Date: 5/15/2014 9:26:50 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetSales]
(
@pBeginDate DateTime = NULL,
@pEndDate DateTime = NULL
)
AS
BEGIN
select H.InvID, H.OrdDate, H.ExpDate, H.PoNumber, H.CustNumber, H.ShippedDate, D.InvPk, D.ProductID,
D.Quantity * D.UnitPrice as Sales, S.SalesRepNumber, S.FullName, C.CustName
from InvHeader H inner join InvDetails D on H.InvID = D.InvID
inner join Customers C on H.CustNumber = C.CustNumber
inner join SalesRep S on C.Salesperson = S.SalesRepNumber
where H.ShippedDate IS NOT NULL
and (H.ShippedDate BETWEEN (@pBeginDate) AND (@pEndDate))
END