Results 1 to 3 of 3

Thread: Third Option for Bit Flag

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Third Option for Bit Flag

    I have a stored procedure similar to this, which returns rows based on whether the IsNewCustomer Bit is set to true or false (hard coded in this sample but called with parameters from a VB app in the original).
    VB Code:
    1. DECLARE @StartDate DateTime, @EndDate DateTime, @IsNewCust bit;
    2. SET @StartDate = '2007-09-01 00:00:00'
    3. SET @EndDate = '2007-09-30 23:59:59'
    4. SET @IsNewCust = 0
    5.  
    6. SELECT * FROM Tickets
    7. WHERE    
    8.     (Tickets.SaleDate BETWEEN @StartDate AND @EndDate) AND
    9.     (Tickets.IsNewCustomer = @IsNewCust)
    Right now when I run this procedure I have to pass the IsNewCust parameter to specify which records I want back, which works great but I also want a third option to get all the records (both 0's and 1'’s).

    How can that be done when the stored procedure is always looking for the parameters it contains. Is there a way to set optional parameters in SQL?

    By the way this is SQL Server 2005 if that makes a difference.

  2. #2
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Third Option for Bit Flag

    Optional parameter in the example below is @param3 with a default of 0:
    Code:
    create procedure MyProc @param1 int, @param2 int, @param3 bit=0
    as
    Back to your main question. I suggest you specify the @IsNewCust param with default value of NULL. In the sproc body you do an IF test for @IsNewCust IS NULL and execute without the IsNewCustomer filter

    Code:
    IF @IsNewCust IS NULL
    BEGIN
         SELECT * FROM TicketsWHERE         (Tickets.SaleDate BETWEEN @StartDate AND @EndDate)
    END
    ELSE
    BEGIN
         SELECT * FROM TicketsWHERE         (Tickets.SaleDate BETWEEN @StartDate AND @EndDate) AND     (Tickets.IsNewCustomer = @IsNewCust)
    END

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Third Option for Bit Flag

    or...

    Use some bit-logic

    1 - new customer
    2 - not a new customer
    3 - both new and not new customers

    Code:
    (Tickets.IsNewCustomer = 0 and (@IsNewCust & 1)<>0)
    or (Tickets.IsNewCustomer <> 0 and (@IsNewCust & 2)<>0)
    I never liked those bit type fields myself - why are you using them??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width