?
Solved

sql stored procedure

Posted on 2006-04-12
3
Medium Priority
?
1,254 Views
Last Modified: 2008-01-09
I need to create a stored procedure named spDateRange that accepts two parameters, @DateMin and @DateMax, with data type varchar and default value null. If called with no parameters or with null values, the procedure should return an error message describing the syntax. If called with non-null values, validate the parameters. Test that the literal strings are valid dates and test that @DateMin is earlier than @DateMax. If the parameters are valid, 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 earliest invoice first.

I ma having problem with the message to show the error while checking the syntaxt , I can not get prcedure to read the insturction another words multi statement table to return instructions.
0
Comment
Question by:tagtekin
  • 2
3 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 16442381
CREATE Procedure spDateRange
            @DateMin varchar(20) = Null,
            @DateMax varchar(20) = Null

As

SET NOCOUNT ON

Declare @Error tinyint

If @DateMin Is Null Or @DateMax Is Null
      Return 1

If ISDATE(@DateMin) = 0 Or ISDATE(@DateMax) = 0
      Return 2

If CAST(@DateMin as datetime) >= CAST(@DateMax as datetime)
      Return 3


Select      InvoiceNumber,
      InvoiceDate,
      InvoiceTotal,
      Balance
From      Invoices
Where      InvoiceDate BETWEEN CAST(@DateMin as datetime) AND CAST(@DateMax as datetime)
Order By InvoiceDate

Return 0
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 200 total points
ID: 16442386
Let's try that again (I left a testing line)

CREATE Procedure spDateRange
            @DateMin varchar(20) = Null,
            @DateMax varchar(20) = Null

As

SET NOCOUNT ON

If @DateMin Is Null Or @DateMax Is Null
      Return 1

If ISDATE(@DateMin) = 0 Or ISDATE(@DateMax) = 0
      Return 2

If CAST(@DateMin as datetime) >= CAST(@DateMax as datetime)
      Return 3

Select      InvoiceNumber,
      InvoiceDate,
      InvoiceTotal,
      Balance
From      Invoices
Where      InvoiceDate BETWEEN CAST(@DateMin as datetime) AND CAST(@DateMax as datetime)
Order By InvoiceDate

Return 0
0
 

Author Comment

by:tagtekin
ID: 16442442
thanks thats works
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
What we learned in Webroot's webinar on multi-vector protection.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

862 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