|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25,
Visits: 51
|
|
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 PROC sp_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
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Saturday, May 04, 2013 11:13 AM
Points: 9,855,
Visits: 9,374
|
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 3:18 PM
Points: 37,744,
Visits: 30,025
|
|
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 2008, MVP 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
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25,
Visits: 51
|
|
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 PROC sp_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'.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 3:18 PM
Points: 37,744,
Visits: 30,025
|
|
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 2008, MVP 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
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25,
Visits: 51
|
|
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'
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25,
Visits: 51
|
|
Nevermind, I got some results with this exec: Exec spDateRange '20060501','20060530'
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 3:18 PM
Points: 37,744,
Visits: 30,025
|
|
I'd guess there's no data for the period in 2008 within the invoices table.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP 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
|
|
|
|