SQL variable restriction

Posted on 2014-08-22
Last Modified: 2014-08-25
I'm creating a stored proc with a variable @AsOfDate as date. Now, I only want to accept the input date if it is within 3 days of today, otherwise it should not accept it and throw an error. Currently I'm doing this with a IF/ELSE statement. Is there a better way of handling this?

I'm using SQL 2012
Question by:timberadmin
    LVL 65

    Accepted Solution

    Something like...
    CREATE PROC your_proc (@AsOfDate date) AS
    IF ABS(DATEDIFF(day,@AsOfDate, CAST(GETDATE() as date)))  > 3  
       SELECT 'error'
       SELECT 'your proc goes here'

    Open in new window


    Author Comment

    So that's exactly how I'm doing it right now. I assume this is the correct and most efficient way of handling it?
    LVL 65

    Expert Comment

    by:Jim Horn
    I'm sure there are other ways, but this is the way I'd do it.

    btw Did you really want to throw an error, which would involve RAISEERROR, or just handle it?

    Author Comment

    If this stored proc is run from Excel VBA, I want the user to see the error message I give.
    LVL 44

    Assisted Solution

    by:Vitor Montalvão
    Then I think it's better you raise an SQL Server error. Something like this:

    RAISERROR (15999,-1,-1, 'The date difference is higher than 3 days');

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    Viewers will learn how the fundamental information of how to create a table.
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now