Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old October 1st, 2004, 02:56 PM
brandon1974 brandon1974 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 7 brandon1974 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Local variables in a view

Hello everyone. I need to set the value of a column from the value of another column in a view, like so:

create view vBoatMetrics
as
select PK_BID,
CASE when(var1 > 1) then A else B end as Column1Val
CASE when(var2 > 1) then C else D end as Column2Val

This part works fine, but when I try to:

declare @Variable float
set @Variable = (Column1Val*Column2Val),
Column3Val = @Variable

I get errors. Can someone please show me how to create a local variable in a view so I can carry the value throught the view? I have several calculations in a view that need to be based on the outcomes of other calculations and so forth. I would GREATLY appreciate some help!

MANY TIA,
Brandon

Reply With Quote
  #2  
Old October 1st, 2004, 03:01 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,760 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 5 h 22 m 16 sec
Reputation Power: 443
Have you tried something like this?
Code:
SELECT PK_BID,
CASE when(var1 > 1) then A else B end as Column1Val,
CASE when(var2 > 1) then C else D end as Column2Val,
(Column1Val * Coumn2Val) As Column3Val

Reply With Quote
  #3  
Old October 1st, 2004, 03:06 PM
brandon1974 brandon1974 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 7 brandon1974 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yes. I get an Error 207: Invalid column name 'Column1Val', invalid column name 'Column2Val'



Quote:
Originally Posted by Memnoch
Have you tried something like this?
Code:
SELECT PK_BID,
CASE when(var1 > 1) then A else B end as Column1Val,
CASE when(var2 > 1) then C else D end as Column2Val,
(Column1Val * Coumn2Val) As Column3Val

Reply With Quote
  #4  
Old October 1st, 2004, 03:15 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,760 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 5 h 22 m 16 sec
Reputation Power: 443
post your table structure and some sample data.

Reply With Quote
  #5  
Old October 1st, 2004, 03:21 PM
brandon1974 brandon1974 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 7 brandon1974 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok, only one table. P and E are boat measurements in feet.

tbl_Boats
---------
PK_BID int, id, not null
P decimal 5,2
E decimal 5,2

So, we would have

PK_BID P E
1 10.5 12.78
2 9.76 11.97

So, in my view I'm performing some caluclations to turn feet into meters:
(P*.3048) AS Pm
(E*.3048) AS Em

But when I try to say
(Pm*Em) AS Something

I get the error. Very frustrating! It works in Access, but not SQL Server.

- - -

Quote:
Originally Posted by Memnoch
post your table structure and some sample data.

Reply With Quote
  #6  
Old October 1st, 2004, 03:30 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,760 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 5 h 22 m 16 sec
Reputation Power: 443
How about this?
Code:
SELECT pk_BID,
	(P * .3048) As Pm,
	(E * .3048) As Em,
	((P * .3048) * (E * .3048)) As Total
FROM tblBoats


Returns this
Code:
1  3.200400  3.895344  12.466658937600
2  2.974848  3.648456  10.853602034688

Reply With Quote
  #7  
Old October 1st, 2004, 03:33 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,760 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 5 h 22 m 16 sec
Reputation Power: 443
More detailed would be this
Code:
SELECT pk_BID,
	Cast((P * .3048) As dec(5,2)) As Pm,
	Cast((E * .3048) As dec(5,2)) As Em,
	Cast(((P * .3048) * (E * .3048)) As dec(5,2)) As Total
FROM Table1

Which displays this
Code:
1  3.20  3.90  12.47
2  2.97  3.65  10.85

Reply With Quote
  #8  
Old October 1st, 2004, 03:42 PM
brandon1974 brandon1974 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 7 brandon1974 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ugh. So I'm going to have to go through each calculation before I can arrive at my total instead of being able to call a local variable that represents that total?



Quote:
Originally Posted by Memnoch
More detailed would be this
Code:
SELECT pk_BID,
	Cast((P * .3048) As dec(5,2)) As Pm,
	Cast((E * .3048) As dec(5,2)) As Em,
	Cast(((P * .3048) * (E * .3048)) As dec(5,2)) As Total
FROM Table1

Which displays this
Code:
1 3.20 3.90 12.47
2 2.97 3.65 10.85

Reply With Quote
  #9  
Old October 1st, 2004, 03:46 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,760 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 5 h 22 m 16 sec
Reputation Power: 443
Views don't allow the use of variables, stored procedures do.
but you would still have to do this
Code:
Declare @total decimal(5,2)

Set @total = Cast(((P * .3048) * (E * .3048)) As dec(5,2)) 

Reply With Quote
  #10  
Old October 1st, 2004, 03:48 PM
brandon1974 brandon1974 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 7 brandon1974 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
So can I get a value from a stored procedure and pull it into a view as a column?



Quote:
Originally Posted by Memnoch
Views don't allow the use of variables, stored procedures do.

Reply With Quote
  #11  
Old October 1st, 2004, 03:49 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,760 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 5 h 22 m 16 sec
Reputation Power: 443
If you need to do it that way, then just create a stored procedure instead of a view.

Reply With Quote
  #12  
Old October 1st, 2004, 03:53 PM
brandon1974 brandon1974 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 7 brandon1974 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I'm not very familiar with stored procedures at all. How would I get the values out of the stored procedure so I could display them on an ASP page? Can I use a stored procedure as a datasource to create a recordset (or whatever the stored procedure's equivalent to a recordset is)?



Quote:
Originally Posted by Memnoch
If you need to do it that way, then just create a stored procedure instead of a view.

Reply With Quote
  #13  
Old October 2nd, 2004, 10:33 AM
brandon1974 brandon1974 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 7 brandon1974 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Actually what I ended up doing was creating user-defined functions to perform the calculations. Then, starting from the outside and working in, nested the functions within other functions in my view to create the calculations that I needed. Works like a charm! I didn't know I could create a column as the outcome of a function in a table, but you can, which helps my situation tremendously. This way I only have one view that returns all of the calculations.

Thanks for your help, it got me thinking in the right direction!
Brandon


Quote:
Originally Posted by brandon1974
I'm not very familiar with stored procedures at all. How would I get the values out of the stored procedure so I could display them on an ASP page? Can I use a stored procedure as a datasource to create a recordset (or whatever the stored procedure's equivalent to a recordset is)?

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Local variables in a view


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump