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 |
jjmagro
Starting Member
5 Posts |
Posted - 2010-11-18 : 18:38:17
|
Ok, maybe I wasn't very descriptive in the Subject of this post, the problem is simple, but I can't have any work around that actually works :(I have a master and detail table and I want to return all the info in the master even if they don't exists in the detail tableSELECT m.Concept, sum(d.someInteger) FROM detailsTable dOUTER JOIN mainTable mON m.someId = d.someId I tried LEFT OUTER, RIGHT OUTER, I change the order of the tables queried, but always returns all the matches in detail table, (9 out of 10)I was taking a look at some blogs about doing JOINS but nothing work so far...I would like to put the code of the stored procedure here, but is quite large but let me tell you something about it:I'm evaluating some parameter, and as a result, I'm creating a query as a VARCHAR, at the end, I exec the VARCHAR variable resultant query.The stored procedure is something like this:CREATE PROCEDURE mySP @someParam VARCHAR(2)ASDECLARE @bigQuery VARCHAR(MAX)SET @bigQuery = 'SELECT m.mainConcept, 'IF(@someParam = 'A') SET @bigQuery = @bigQuery + 'SUM(d.someValue1) 'ELSE IF(@someParam = 'B')BEGIN SET @bigQuery = @bigQuery + 'SUM(d.someValue1) + ' SET @bigQuery = @bigQuery + 'SUM(d.someValue2) 'ENDSET @bigQuery = @bigQuery + 'FROM detailTable d 'SET @bigQuery = @bigQuery + 'LEFT OUTER JOIN masterTable m 'SET @bigQuery = @bigQuery + 'ON m.someId = d.someMasterId 'SET @bigQuery = @bigQuery + 'GROUP BY m.mainConcpt 'EXEC (@bigQuery) The result is always the same, doesn't matter if I put "LEFT OUTER", "RIGHT OUTER", or even "INNER JOIN", "LEFT JOIN" or whatever.By the way, the tables that I'm using in the query, are not indexed, nor have primary keys (I don't know if this could be the problem)...So, if someone can bring me some light about this, I really appreciate it, any help is welcome :) |
|
X002548
Not Just a Number
15586 Posts |
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|
|