?
Solved

SQL variable restriction

Posted on 2014-08-22
5
Medium Priority
?
196 Views
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
0
Comment
Question by:timberadmin
  • 2
  • 2
5 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 1400 total points
ID: 40279615
Something like...
CREATE PROC your_proc (@AsOfDate date) AS

IF ABS(DATEDIFF(day,@AsOfDate, CAST(GETDATE() as date)))  > 3  
   begin
   SELECT 'error'
   end 
ELSE
   begin
   SELECT 'your proc goes here'
   end

Open in new window

0
 

Author Comment

by:timberadmin
ID: 40279622
So that's exactly how I'm doing it right now. I assume this is the correct and most efficient way of handling it?
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40279631
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?
0
 

Author Comment

by:timberadmin
ID: 40279637
If this stored proc is run from Excel VBA, I want the user to see the error message I give.
0
 
LVL 53

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 600 total points
ID: 40282680
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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

807 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