Solved

how to declare variables in a view

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modifying SQL 2008/2012 PARTITIONS 3 55
How to enforce inte 8 43
SQL Trigger selecting another database 4 34
Parsing the XML data to SQL Server 4 59
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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

20 Experts available now in Live!

Get 1:1 Help Now