Stored procedure not returning all the rows expected
-
below is the stored procedure I’m having problems with. Its purpose is to filter the results within a result set returned by the spGetTicketList stored procedure, which works as expected. If I remove the entire
WHERE
cluse section from the stored procedure I get 458545 rows returned, likewise if I include the Ticket Number range filters. If I then add the Date range filter I get 446726 rows returned and if I include all the range filters I get 415179 rows returned. I am not providing any values for the parameters, other than the default values. With all the filters in place there is 43,366 rows missing. Any ideas?ALTER PROCEDURE [dbo].[spFilterTicketList]
(
@LowTicketNumber VarChar(MAX) = '',
@HighTicketNumber VarChar(MAX) = 'zzzzzzzzzzzzzzzzzzzzz',
@LowDate DateTime = null,
@HighDate DateTime = null,
@AccountNumber varchar(MAX) = '%',
@AcccountName varchar(MAX) = '%',
@ShortName varchar(MAX) = '%',
@StoreId varchar(MAX) = '%',
@RoundId varchar(MAX) = '%'
)
AS
BEGIN
DECLARE @t1 as Table
(
Id bigint,
[Ticket Number] varchar(50),
[Account Number] varchar(50),
[Account Name] varchar(120),
[Short Name] varchar(50),
[Store Id] varchar(50),
[Date] DateTime,
[Round Id] varchar(50),
Source tinyint
);INSERT @t1 EXEC spGetTicketList; IF(@LowDate IS NULL) BEGIN SET @LowDate = CAST('1753-01-01 00:00:00.000' As DATE); END IF(@HighDate IS NULL) BEGIN SET @HighDate = CAST('9999-12-31 23:59:59.997' As DATE); END SELECT Id, ISNULL(\[Ticket Number\], '') AS \[Ticket Number\] , ISNULL(\[Account Number\] , ' ') AS \[Account Number\], ISNULL(\[Account Name\] , ' ') AS \[Account Name\], ISNULL(\[Short Name\] , ' ') AS \[Short Name\], ISNULL(\[Store Id\] , ' ') AS \[Store Id\], CAST(ISNULL(\[Date\], CAST('1753-01-01 00:00:00.000' As DateTime)) As DateTime) AS \[Date\], ISNULL(\[Round Id\] , ' ') AS \[Round Id\], \[Source\] FROM @t1 WHERE (\[Ticket Number\] >= @LowTicketNumber) AND (\[Ticket Number\] <= @HighTicketNumber) AND (CAST(\[Date\] AS DateTime) >= @LowDate) AND (CAST(\[Date\] AS DateTime) <= @HighDate) AND (\[Account Number\] LIKE @AccountNumber) AND (\[Account Name\] LIKE @AcccountName) AND (\[Short Name\] LIKE @ShortName) AND (\[Store Id\] LIKE @StoreId) AND (\[Round Id\] LIKE @RoundId) ORDER BY \[Date\] ASC ;
END
Steve Jowett ------------------------- Real Programmers don't need comments -- the code is obvious.