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 25th, 2004, 06:33 PM
ned ned is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 3 ned User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy SQL Views - self reference

Hi, I would really appreciate your help on this:

-I have a view vw_VIEW1 with fields AAA and BBB, which are based on some external tables using SELECT Statements.

-trying to calculate a third field CCC so that CCC=somefunction(AAA,BBB), but the view won't let me because
the view columns AAA and BBB are not allowed to be used directly. Error message is that "self-referencing" is not allowed in views.

-Since my fields AAA and BBB each involve a long SELECT query of their own, it's impossible to pass the entire arguments
used to calculate AAA and BBB into somefunction() to calculate CCC.

Is there a way to calculate CCC in the in the view (or in a stored procedure?)
Thanks a lot!

Reply With Quote
  #2  
Old October 25th, 2004, 08:35 PM
Leslie's Avatar
Leslie Leslie is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Honolulu
Posts: 184 Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 20 h 29 m 2 sec
Reputation Power: 9
Can you submit the SQL for View1 with fields AAA and BBB, as well as the code for the function? It's hard to advise when you have not described what it is you are trying to accomplasih ...

Reply With Quote
  #3  
Old October 25th, 2004, 10:37 PM
ned ned is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 3 ned User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Here is one example:


CREATE VIEW dbo.vw_Test
AS

SELECT

Val1,
Val2,
Val3,
(SELECT MAX(Val3) FROM tblTest) AS Max3,
[Val3]*[Max3] AS Result

FROM tblTest


When attempting to store the value [Val3]*[Max3] to a new column 'Result',
SQL says Max3 is not a valid column.
Was wondering if there is a way to use the Value of Max3 in the calculation of other columns

Thanks

Reply With Quote
  #4  
Old October 25th, 2004, 11:23 PM
Leslie's Avatar
Leslie Leslie is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Honolulu
Posts: 184 Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 20 h 29 m 2 sec
Reputation Power: 9
Repeat the SQL you used for Max3 in the next expression

SELECT
Val1,
Val2,
Val3,
(SELECT MAX(Val3) FROM tblTest) AS Max3,
(SELECT [Val3]* MAX(Val3) FROM tblTest) AS Result
FROM tblTest

Something else you can do is in-line select. Most inside SQL is generated first, outer SQL runs based on results of inner like so:

Select Val1, Val2, Val3,Max3, [Max3] * [Val3] as result from
(SELECT
Val1,
Val2,
Val3,
(SELECT MAX(Val3) FROM tblTest) AS Max3,
FROM tblTest
)

So the above is equivalent to first making a view out of the red select statement called view1 and then selecting from it like so:

Select Val1, Val2, Val3, [Max3] * [Val3] as result
from view1
That is why it is called in-line select, you don't actually make a view you just ask the SLQ to execute "in line"

Reply With Quote
  #5  
Old October 26th, 2004, 12:16 AM
ned ned is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 3 ned User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
works great!

Thanks very much!!!

That 2nd case was really useful: since the SELECT statement that
produces the equivalent of variable Max3 is very long in the view i'm building.

Reply With Quote
  #6  
Old October 26th, 2004, 12:22 AM
Leslie's Avatar
Leslie Leslie is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Honolulu
Posts: 184 Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 20 h 29 m 2 sec
Reputation Power: 9
Glad I could help

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > SQL Views - self reference


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


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT