Wednesday, March 7, 2012

Newby JOIN question

I'm attempting to use a JOIN for the first time and I'm getting about 94,000 rows returned when I should only be getting about 270. Something must not be unique (possible DSN) but I can't figure out why the join is not working. I know it must be difficult to determine the problem without actually seeing the tables but could somebody with a lot of SQL experience spot any glaring problems with this query? Thanks!

SELECT DS.DSNName AS DSN,
S.ServerName AS WebServer,
DBs.DBName,
A.ServerName AS SQLServer
FROM DSNs DS,
Servers S,
DBs,
DSNs LEFT OUTER JOIN Servers A
ON DSNs.SQLServerID = S.ServerID
WHERE DBs.Status = 1
AND DS.WebserverID = S.ServerID
AND DBs.DBID = DS.DBID
ORDER BY DSN


Hi reformatted, I got this:

SELECT DS.DSNName AS DSN,
S.ServerName AS WebServer,
DBs.DBName,
A.ServerName AS SQLServer
FROM DSNs DS,
INNER JOIN Servers S,
ON DS.WebserverID = S.ServerID
INNER JOIN DBs
ON DBs.DBID = DS.DBID
INNER JOIN DSNs
ON DSNs.SQLServerID = S.ServerID
LEFT OUTER JOIN Servers A
ON DSNs.SQLServerID = A.ServerID --I guess this was wrong as it was pointing to the S.Server not the A.Server, see if this helps.
WHERE DBs.Status = 1
AND DS.WebserverID = S.ServerID
AND DBs.DBID = DS.DBID
ORDER BY DSN

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||Thanks for your reply. I removed a couple unnecessary commas and the query runs but does not pull back any data. I will play around with it. Please let me know if you have any additional suggestions.|||If I am not sure about a query, I always do a step.by-step approach, starting with the base query and adding more joins one by one controlling the expected results after each step. This is much easier than doing the whole query and then guessing by the results which JOIN was not the right one.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

No comments:

Post a Comment