Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Using variables in a 'View'

Author  Topic 

dordor
Starting Member

5 Posts

Posted - 2011-07-20 : 10:28:42
I'm trying to create a new 'View', and adding the expressions into the 'Column' name to calculate the result for the field, eg:

CASE WHEN CreationDate BETWEEN @StartDate AND @EndDate THEN 1 ELSE 0 END

This works fine in a query. I'm able to save the View also. When I execute the View, it asks me to enter the parameters for @StartDate and @EndDate. No matter what I enter, I get an error saying "The parameter is incorrect." If I replace the variables with a hard-coded date, it works fine, but never by plugging in parameters.

Any ideas how to make this work?

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-07-20 : 10:41:32
Views cannot take parameters, they're single select statements, nothing more. Not sure how you're able to save that view, if I try saving a view with a parameter I get an error

Msg 137, Level 15, State 2, Procedure test, Line 3
Must declare the scalar variable "@StartDate".


--
Gail Shaw
SQL Server MVP
Go to Top of Page

dordor
Starting Member

5 Posts

Posted - 2011-07-20 : 11:15:24
Thank you for your response
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-21 : 04:18:26
dordor which editor where you using for creating view?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -