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 |
|
PGG123
Yak Posting Veteran
55 Posts |
Posted - 2003-03-24 : 15:32:36
|
| My table structures:AccountDist TablelngAccountLevel txtDescription lngStartPos lngLength11 Location 1 512 Division 6 513 Section 11 10FreeKeyAlloc TablelngProfileID txtSegment blnFreeKey2 Location 12 Division 12 Section 12 ACS 1 02 ACS 2 12 ACS 3 1The SP below returns these data:Location 5 1 1Division 5 6 1Section 10 11 1SELECT A.txtDescription, A.lngLength, A.lngStartPos, F.blnFreeKey, A.lngAccountLevelFROM dbo.AccountDist A left outer join dbo.FreeKeyAlloc F on A.txtDescription = F.txtSegmentWHERE (A.lngAccountLevel > 10)AND (F.lngProfileID = 1)ORDER BY A.lngAccountLevel ASCHowever, there will be times when the FreeKeyAlloc table would be empty (no record) and I still want the SP return the data in the AccountDist table. How do I rewrite the SP to cover both situations (FreeKeyAlloc table with or without records in it)?Thanks. |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-03-24 : 17:15:16
|
| SELECT A.txtDescription, A.lngLength, A.lngStartPos, F.blnFreeKey, A.lngAccountLevelFROM dbo.AccountDist A left outer join dbo.FreeKeyAlloc F on A.txtDescription = F.txtSegmentWHERE (A.lngAccountLevel > 10) AND (F.lngProfileID = 1 OR F.lngProfileID IS NULL)ORDER BY A.lngAccountLevel ASC Maybe if you change the WHERE condition on the element in F to include NULL?HTHSam |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-03-24 : 17:44:22
|
SELECT A.txtDescription, A.lngLength, A.lngStartPos, F.blnFreeKey, A.lngAccountLevel FROM dbo.AccountDist A left outer join dbo.FreeKeyAlloc F on A.txtDescription = F.txtSegment AND F.lngProfileID = 1WHERE A.lngAccountLevel > 10ORDER BY A.lngAccountLevel ASC If you have a an outer table in a join, NEVER include that table in your WHERE clause. it turns it into an INNER JOIN. put the criteria in with the join.(also: don't prefix your column names with the datatype; that's SOOOO Visual basic 2.0!!! )- Jeff |
 |
|
|
|
|
|
|
|