Imagine you have a transaction table in which you store the transaction number and transaction date, as shown below.
CREATE TABLE [dbo].[TrnMast]( [TrnNo] [numeric](10, 0) NOT NULL, [AcYear] [int] NOT NULL, [Comp_Code] [varchar](5) NOT NULL, [InvNo] [varchar](20) NULL, [TrnDate] [datetime] NULL, [P_Code] [varchar](5) NULL, [Amount] [money] NULL, [Remark] [varchar](50) NULL CONSTRAINT [PK_TrnMast_1] PRIMARY KEY CLUSTERED ( [TrnNo] ASC, [Comp_Code] ASC, [AcYear] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
Now look at the stored procedure with three parameters that have two date range parameters and TrnNo below.
CREATE PROCEDURE [dbo].[SP_TrnMast] @FTrnDate SmallDateTime = Null, @TTrnDate SmallDateTime = Null, @AcYear Int AS Begin Select * From TrnMast Where (@FTrnDate Is Null Or TrnMast.TrnDate >= @FTrnDate) And (@TTrnDate Is Null Or TrnMast.TrnDate <= @TTrnDate) And TrnMast.AcYear = @AcYear End
source share