Solved

how to declare variables in a view

Posted on 2008-10-20
4
8,025 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
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…

749 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