Solved

simple date range prompt in sql view

Posted on 2011-03-22
6
684 Views
Last Modified: 2012-08-13
I want my view to prompt me for a date range when I run it.  Currently the dates are hard coded.
SELECT     TOP (100) PERCENT tDate, tActivityType, tUnits AS Total
FROM         dbo.AxTransaction
WHERE     (tUnits <> 0) AND (tActivityType = 1) AND (tDate >= CONVERT(DATETIME, '2010-01-01 00:00:00', 102)) AND (tDate <= CONVERT(DATETIME, '2010-12-31 00:00:00', 102))

How do I replace hard coded dates with prompts or parameters?

Thanks
0
Comment
Question by:mbrennan_pacbell_net
6 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 35195778
You cannot do that in view. Create an sp and pass the dates as parameters.
0
 
LVL 9

Expert Comment

by:kaminda
ID: 35195793
You will have to use either a function of sp to do this. My advice is to use an Table Valued Function so it can be used as a view in your queries.

Here is TBV Function script for your view,
FUNCTION [dbo].[fnGetAxTransaction]
(
	@FromDate DateTime,
    @ToDate DateTime
)


RETURNS  TABLE
AS 
RETURN
(
SELECT     TOP (100) PERCENT tDate, tActivityType, tUnits AS Total
FROM         dbo.AxTransaction
WHERE     (tUnits <> 0) AND (tActivityType = 1) AND (tDate >= @FromDate AND (tDate <= @ToDate)
)

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35198549
And please lose the TOP (100) PERCENT, it is pointless.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:mbrennan_pacbell_net
ID: 35200260
kaminda,

Thanks your solution worked somwhat.  I was able to create the table value funcation based on your reply.  However when I try to use it in a view, I get the following error:

"Incorrect Syntax Near ","

I am a SQL nube, so be gentle with me.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35200466
The paranthesis are wrong it should be:
     WHERE  tUnits <> 0
            AND tActivityType = 1
            AND tDate BETWEEN @FromDate AND @ToDate
0
 
LVL 9

Accepted Solution

by:
kaminda earned 125 total points
ID: 35204342
It should change as below as acperkins correctly points out there is a additional paranthesis in the where clause
FUNCTION [dbo].[fnGetAxTransaction]
(
	@FromDate DateTime,
    @ToDate DateTime
)


RETURNS  TABLE
AS 
RETURN
(
SELECT     TOP (100) PERCENT tDate, tActivityType, tUnits AS Total
FROM         dbo.AxTransaction
WHERE     tUnits <> 0 AND tActivityType = 1 AND (tDate >= @FromDate AND tDate <= @ToDate)
)

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# SQL BULK INSERT CLASS 5 36
MS SQL Pagination for Complex Query 5 32
sql calculate averages 18 32
login and database user 3 22
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

744 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

15 Experts available now in Live!

Get 1:1 Help Now