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.
| Author |
Topic |
|
bmsgharris
Starting Member
10 Posts |
Posted - 2006-03-21 : 03:54:58
|
| HiI have a query that needs to perform an outer join on two tables that have an inner join that are in a view. I.e.CREATE VIEW TEST ASSELECT t1.col1, t2.col2FROM table1 t1, table2 t2WHERE t2.col1 = t1.col1SELECT * FROM table3 t3, test t WHERE t.col2 =* t3.t2idWhen I run the query I get the following error:Msg 303, Level 16, State 1, Line 1The table 'table2' is an inner member of an outer-join clause. This is not allowed if the table also participates in a regular join clause.Now my understanding of a view is that it is like a table but built from the underlying select statement, i.e. the SQL Parser treats the view as a table, and not insert the view in the SQL Statement. Also running Profiler I get the exact same query being run. This is because I want to avoid using a temporary table. I am using SQL Server 2000 SP4TIAGraham Harris |
|
|
mwjdavidson
Aged Yak Warrior
735 Posts |
Posted - 2006-03-21 : 06:24:05
|
I'd get into the habit of using ANSI syntax for joins. It may well be that this solves your problem, as I've never had a similar problem before. In addition, you could replace the view with either a derived table or a nested join depending on the amount of data you're dealing with, etc.:-- derived tableSELECT t1t2.col1, t1t2.col2, t3.*FROM table3 AS t3LEFT JOIN ( SELECT t1.col1, t2.col2 FROM table1 AS t1 JOIN table2 AS t2 ON t1.col1 = t2.col1 ) AS t1t2ON t3.t2id = t1t2.col2--nested joinSELECT t1.col1, t2.col2, t3.*FROM table3 AS t3LEFT JOIN table1 AS t1JOIN table2 AS t2ON t1.col1 = t2.col1ON t3.t2id = t2.col2 Mark |
 |
|
|
bmsgharris
Starting Member
10 Posts |
Posted - 2006-03-21 : 06:34:13
|
| The trouble is that I find that using inner/outer join syntax as I find it confusing!!! Is there any decent resource that can explain the syntax etc. clearly? The only reason I ask is that I learnt my SQL in Oracle and Oracle, at the time, did not support the inner/outer join syntax.ThanksGraham Harris |
 |
|
|
mwjdavidson
Aged Yak Warrior
735 Posts |
Posted - 2006-03-21 : 08:21:58
|
| Not sure about online resources (other than Books Online), but any good SQL book will cover this syntax in some detail. It's a pretty straightforward process to move from one to the other. Your WHERE clause becomes the ON part of the join. and instead of '*' you specify the join as being INNER or LEFT OUTER (most people tend to standardise on LEFT OUTER joins and don't use RIGHT OUTER joins). NB you can do without the INNER and OUTER keywords and just use JOIN and LEFT. So:SELECT *FROM a, bWHERE a.id = b.idbecomesSELECT *FROM aJOIN bON a.id = b.idSELECT *FROM a,bWHERE a.id = * b.idbecomesSELECT *FROM b LEFT JOIN aON b.id = a.idIf you have no constraint in the where, that becomes a CROSS JOIN, i.e.:SELECT *FROM a, bbecomesSELECT *FROM aCROSS JOIN bNot sure how to do a FULL JOIN in the old syntax, and I can't experiment as I'm using SQL Server 2005 and it's not supported without changing compatibility settings (another good reason to stop using it!)Mark |
 |
|
|
|
|
|
|
|