Solved

how to declare variables in a view

Posted on 2008-10-20
4
8,031 Views
Last Modified: 2010-04-21
I have a sql query in a view which I would like to use variables.

(@start_date datetime, @end_date datetime)

when I run the query i get an error saying;

must declare scalar variable @start_date.

I am not sure what this means?
0
Comment
Question by:Mr_Shaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:brad2575
ID: 22757813
you cant,

You can create a user defined function OR a stored procedure and pass the variables to that and have either the UDF or SP return a record set.

I just did this using a stored procedure and it worked great for me.
0
 

Author Comment

by:Mr_Shaw
ID: 22757839
can I have three stored procedures and link them together with a cross join?
0
 
LVL 16

Accepted Solution

by:
brad2575 earned 500 total points
ID: 22757908
nope, tried that as well.

What you can do is either of the following (I have done both recently):

1.  Create a generic view(s) for your queries without the varaible/criteria and then in the SP do the cross join there and add the criteria there.  

2.  Just create all 3 queries directly in the SP and do the cross join there with the criteria.

NOTE:  You CAN create a view and then add critera when you select FROM the view.

So if you have a veiw called vw_TestView you can do this in your select statement from the view:

select * from vw_TestView
Where field1 = 'test'
and fiedl2 = 2

0
 

Author Closing Comment

by:Mr_Shaw
ID: 31507816
Thanks, will give it a go.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

627 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