|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
Yes. I get an Error 207: Invalid column name 'Column1Val', invalid column name 'Column2Val'
Quote:
|
|
#4
|
||||
|
||||
|
post your table structure and some sample data.
|
|
#5
|
|||
|
|||
|
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:
|
|
#6
|
||||
|
||||
|
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 |
|
#7
|
||||
|
||||
|
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 |
|
#8
|
|||
|
|||
|
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:
|
|
#9
|
||||
|
||||
|
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)) |
|
#10
|
|||
|
|||
|
So can I get a value from a stored procedure and pull it into a view as a column?
Quote:
|
|
#11
|
||||
|
||||
|
If you need to do it that way, then just create a stored procedure instead of a view.
|
|
#12
|
|||
|
|||
|
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:
|
|
#13
|
|||
|
|||
|
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:
|
![]() |
| Viewing: ASP Free Forums > Database > Microsoft SQL Server > Local variables in a view |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|