Stored procedure- help with date parameter validation

  • Hello,

    I am creating a stored procedure that accepts two parameters,

    @DateMin and @DateMax, ea.with data type varchar and default value null.

    1.If called with no parameters or with null values, the procedure should return an error explaining the needed parameters.

    2. If called with non-null values, I need to validate the parameters to ensure that the @DateMin is earlier than @DateMax, if valid return the result set, if invalid then display an error message.

    This is what I have so far. I am stuck on how to proceed. Do I need Print statements?

    Thanks for any help.

    CREATE PROCsp_DateRange

    @DateMin varchar = NULL

    @DateMax varchar = NULL

    If (@DateMin is null)

    begin

    Raiserror('Please enter the MinimumDate ',16,1)

    Return

    If (@DateMax is null)

    begin

    Raiserror('Please enter the MaximumDate ',16,1)

    Return

    If (@DateMin < @DateMax)

    begin

    Raiserror('Minimum Date can’t be later then maximum date ',16,1)

    Return

    If @DateMin < @DateMax

    Begin

    (Select InvoiceNumber, InvoiceDate, InvoiceTotal, InvoiceTotal-PaymentTotal-CreditTotal AS Balance

    From Invoices

    Order By InvoiceDate

    END

    Exec sp_DateRange 10-1-2008, 10-15-2008

  • Why are you using Varchar's for your dates? why not use Datetime datatype?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • t-pinto (11/29/2008)


    If @DateMin < @DateMax

    Begin

    (Select InvoiceNumber, InvoiceDate, InvoiceTotal, InvoiceTotal-PaymentTotal-CreditTotal AS Balance

    From Invoices

    Order By InvoiceDate

    END

    The rest looks good (other than the varchar types)

    What do you want to do with the resultset and the dates? I assume they should limit the returned rows. How?

    Also, regarding varchar, DECLARE @SomeVar varchar will get you a varchar with length 1. Always define the length with a varchar columns. Helps avoid surprises.

    I'd also suggest you pass the dates in an unambiguous form - yyyy-mm-dd, because the interpretation of the one you used differs in different places. Where I live, the two dates you passed are the 10th of January and invalid (there is no 15th month)

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I made some modifications to the code but cont to get an error message when creating the procedure. Here is the latest code and my error.

    CREATE PROCsp_DateRange

    @DateMin DateTime = NULL,

    @DateMax DateTime = NULL

    AS

    If (@DateMin is null)

    begin

    Raiserror('Please enter the MinimumDate ',16,1)

    Return

    End

    If (@DateMax is null)

    begin

    Raiserror('Please enter the MaximumDate ',16,1)

    Return

    End

    If (@DateMin > @DateMax)

    begin

    Raiserror('Minimum Date can’t be later then maximum date ',16,1)

    Return

    End

    If @DateMin < @DateMax

    Begin

    (Select InvoiceNumber, InvoiceDate, InvoiceTotal, InvoiceTotal-PaymentTotal-CreditTotal AS Balance

    From Invoices

    Where InvoiceDate >= @DateMin and InvoiceDate < @DateMax+1

    Order By InvoiceDate

    END

    --Error Message

    Incorrect syntax near the keyword 'Order'.

  • If @DateMin < @DateMax

    Begin

    (Select InvoiceNumber, InvoiceDate, InvoiceTotal, InvoiceTotal-PaymentTotal-CreditTotal AS Balance

    That bracket right after the begin that shouldn't be there.

    InvoiceDate < @DateMax+1

    Meaning 1 day later than the datemax that the user specified?

    You shouldn't name procs sp_. That's for system stored procs and means that SQL will look first in one of the system databases to see if your proc is there

    What should happen if Datemin = DateMax? Currently the proc will do nothing if that happens.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I modified the parameter datatypes, stored procedure naming convention and added a rule to take into account the mindate = max date.

    The procedure was created without errors.

    If the parameters are valid I would like to return a result set that includes the InvoiceNumber, InvoiceDate, InvoiceTotal and Balance for each invoice for which the InvoiceDate is within the date range, sorted with the earliest invoice first.

    I executed my procedure as follows with 2 date parameters but did not get any results.

    exec spDateRange '2008-07-01', '2008-08-01'

  • Nevermind, I got some results with this exec:

    Exec spDateRange '20060501','20060530'

  • I'd guess there's no data for the period in 2008 within the invoices table.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I had the same problem but was supposed to have 5 row(s) affected & the command ran successfully - but with 0 row(s) affected....:o(

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply