Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Stored procedure not returning all the rows expected

Stored procedure not returning all the rows expected

Scheduled Pinned Locked Moved Database
databasequestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steven J Jowett
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups