September 28, 2008 at 4:18 pm
I have the following stored procedure:
SELECT Homes.ListingID, Homes.FullAddress, Contact.Person, Contact.Email, Contact.Phone, Essentials.Beds, Essentials.Baths, Essentials.Style, Essentials.Footage, Essentials.Description, Features.Features, Financials.Price, Financials.Maint, Financials.Taxes, Financials.Down FROM Homes INNER JOIN Contact ON Homes.ListingID=Contact.ListingID INNER JOIN Essentials ON Contact.ListingID=Essentials.ListingID INNER JOIN Features ON Essentials.ListingID=Features.ListingID INNER JOIN Financials ON Financials.ListingID=Features.ListingID WHERE Homes.ListingID=@listid
When I run the procedure in sql server, I get no values returned with none of the listid I have.
However, when I look into the tables, there is data for every listid.
Yet, if I run another stored procedure, they return values flawlessly.
What is it about this stored procedure that no values are being returned even though the data is in the tables? Has anyone experienced this?
Any help will be appreciated.
Thanks.
September 28, 2008 at 4:34 pm
Could the inner joins be filtering out the rows?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
September 28, 2008 at 7:16 pm
In other words, start by doing an OUTER JOIN on the homes table with all the other tables. Then, wittle from there.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 29, 2008 at 1:15 am
Copy the query in the query window and write the valid listID (against which data exists) replacing @listid and then execute it.
I am sure it will not give the result. If it give the result against the same id you are providing to the Sp as parameter, than there IS a problem...
Atif Sheikh
September 29, 2008 at 3:26 am
Hi Mark
As Gail and Jeff have already suggested, change your joins to LEFT JOINS:
[font="Courier New"]SELECT h.ListingID, h.FullAddress,
c.ListingID, c.Person, c.Email, c.Phone,
e.ListingID, e.Beds, e.Baths, e.Style, e.Footage, e.[Description],
t.ListingID, t.Features,
f.ListingID, f.Price, f.Maint, f.Taxes, f.Down
FROM Homes h
LEFT JOIN Contact c ON c.ListingID = h.ListingID
LEFT JOIN Essentials e ON e.ListingID = h.ListingID
LEFT JOIN Features t ON t.ListingID = h.ListingID
LEFT JOIN Financials f ON f.ListingID = h.ListingID
WHERE h.ListingID = @listid
[/font]
Note that since ListingID values are perpetuated throughout all of the tables, they can all, for the purposes of this query, be considered child tables of the parent table Homes. In your original query, if you had a missing row in the Essentials table then you would also lose any matching rows in the Features and Financials tables. I've taken the liberty of adding ListingID for each table into the output to show which tables have a matching row and which do not. I've also added table aliases to make the query a little more readable.
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply