gt6989b
gt6989b

Reputation: 4203

Stored Procedure unexpectedly returns 2 results occasionally

I have the following stored procedure to fetch data from a table based on 2 parameters:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:      Dmitry Kreslavskiy
-- Create date: 2015-01-12
-- Description: spGetCreditBenchmarkCurves
-- =============================================

/* spGetCreditBenchmarkCurves
 * Get the list of credit benchmark spread records, if necessary fixing
 * currency and latest ValueDate <= RunDate.
 *     \param[in]  RunDate    Run date of the search (uses the last date up to
 *                            this time), NULL = all
 *     \param[in]  Ccy        Use only this currency
 *     \return     Table (Ccy, ValueDate, TenorSize, TenorUnit, Value)
 */

ALTER PROCEDURE [dbo].[spGetCreditBenchmarkCurves]
(
    @RunDate date       = NULL,
    @Ccy     varchar(3) = NULL
)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    IF @RunDate IS NULL
       IF @Ccy IS NULL
           SELECT *
             FROM [dbo].CreditBenchmarkCurves
       ELSE -- @Ccy is supplied
             SELECT *
               FROM [dbo].CreditBenchmarkCurves
              WHERE Ccy = @Ccy
    ELSE -- @RunDate is supplied
       /* It could be that the table does not have any valid data on @RunDate,
        * so find the latest date before @RunDate with valid data, and store
        * this in @ExactDate, to use it in a query directly.
        *
        * (Same thing could be done using an INNER JOIN instead of 2 selects,
        *  but it is much clearer code to do it step by step. Also, I have a
        *  feeling this implementation is faster as well.)
        */
       DECLARE @ExactDate date
       SET @ExactDate = (SELECT MAX(ValueDate)
                           FROM [dbo].CreditBenchmarkCurves
                          WHERE ValueDate <= @RunDate)
       IF @Ccy IS NULL
           SELECT *
             FROM [dbo].CreditBenchmarkCurves
            WHERE ValueDate = @ExactDate
       ELSE -- @Ccy is supplied
           SELECT *
             FROM [dbo].CreditBenchmarkCurves
            WHERE ValueDate = @ExactDate AND
                  Ccy       = @Ccy
END

GO

This code occasionally returns one table of values, as expected:

EXEC dbo.spGetCreditBenchmarkCurves @RunDate = '2015-02-27', @Ccy = 'AUD'

But passing the default run date gives one full table and one empty table:

EXEC dbo.spGetCreditBenchmarkCurves @RunDate = NULL, @Ccy = 'AUD'
EXEC dbo.spGetCreditBenchmarkCurves

What is the bug, how can 2 SELECT statements be executed with such code? Thank you

Upvotes: 0

Views: 78

Answers (2)

Giorgi Nakeuri
Giorgi Nakeuri

Reputation: 35780

This code

ALTER PROCEDURE [dbo].[spGetCreditBenchmarkCurves]
(
    @RunDate date       = NULL,
    @Ccy     varchar(3) = NULL
)
AS
BEGIN
    IF @RunDate IS NULL
       IF @Ccy IS NULL
           SELECT *
             FROM [dbo].CreditBenchmarkCurves
       ELSE -- @Ccy is supplied
             SELECT *
               FROM [dbo].CreditBenchmarkCurves
              WHERE Ccy = @Ccy
    ELSE 
       DECLARE @ExactDate date
       SET @ExactDate = (SELECT MAX(ValueDate)
                           FROM [dbo].CreditBenchmarkCurves
                          WHERE ValueDate <= @RunDate)
       IF @Ccy IS NULL
           SELECT *
             FROM [dbo].CreditBenchmarkCurves
            WHERE ValueDate = @ExactDate
       ELSE -- @Ccy is supplied
           SELECT *
             FROM [dbo].CreditBenchmarkCurves
            WHERE ValueDate = @ExactDate AND
                  Ccy       = @Ccy
END

is equal to:

ALTER PROCEDURE [dbo].[spGetCreditBenchmarkCurves]
(
    @RunDate date       = NULL,
    @Ccy     varchar(3) = NULL
)
AS
BEGIN
    IF @RunDate IS NULL
    BEGIN
       IF @Ccy IS NULL
       BEGIN
           SELECT * FROM [dbo].CreditBenchmarkCurves
       END
       ELSE BEGIN
           SELECT * FROM [dbo].CreditBenchmarkCurves WHERE Ccy = @Ccy
       END
    END
    ELSE BEGIN
       DECLARE @ExactDate date
    END

   SET @ExactDate = (SELECT MAX(ValueDate)
                       FROM [dbo].CreditBenchmarkCurves
                      WHERE ValueDate <= @RunDate)
   IF @Ccy IS NULL
       SELECT *
         FROM [dbo].CreditBenchmarkCurves
        WHERE ValueDate = @ExactDate
   ELSE -- @Ccy is supplied
       SELECT *
         FROM [dbo].CreditBenchmarkCurves
        WHERE ValueDate = @ExactDate AND
              Ccy       = @Ccy

END

Without the BEGIN END only DECLARE @ExactDate date is considered as ELSE block. So actually if @RunDate IS NULL select will occur 2 times.

Upvotes: 2

Khurram Ali
Khurram Ali

Reputation: 1679

Wrapp your if else condition with begin/end

IF @RunDate IS NULL
BEGIN 
 /* your code for if */
END
ELSE
BEGIN
/* your code for else */
END

Note:

  1. BEGIN and END define a series of Transact-SQL statements that execute together.
  2. IF...ELSE construct can be used in batches, in stored procedures, and in ad hoc queries

Reference 1: MSDN IF ELSE

Reference 2: MSDN IF ELSE WITH BEGIN END

Upvotes: 2

Related Questions