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 |
ATG
Starting Member
35 Posts |
Posted - 2012-08-23 : 16:04:06
|
I'm working on a stored procedure that dumps every day between 2 provided dates into a temp table, named #PMDailyLogs. Once that table is created, I'm joining it to an existing table which has some matching values but not all. Here is the code:SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[DCCMonth]( @BegDate bDate, @EndDate bDate, @Project bProject)ascreate table #PMDailyLogs ( LogDate DATETIME NULL ) declare @dt datetimeset @dt = @BegDatewhile @dt<=@EndDateBEGIN insert #PMDailyLogs values (@dt) set @dt = DATEADD(DAY, 1, @dt)ENDselect #PMDailyLogs.LogDatefrom #PMDailyLogsLEFT OUTER JOIN PMDD on PMDD.LogDate=#PMDailyLogs.LogDatewhere PMDD.Project='120127.' and PMDD.PMCo=1order by #PMDailyLogs.LogDatedrop table #PMDailyLogs;I'm stumped because when the PMDD table is not included in the code, everyday is shown, however when I do include PMDD (joined on LogDate for both tables) it only shows values that exist in PMDD. It seems the OUTER clause is not doing anything.Thoughts? |
|
chadmat
The Chadinator
1974 Posts |
Posted - 2012-08-23 : 16:11:25
|
Make this: PMDD.LogDate=#PMDailyLogs.LogDatelook like this: #PMDailyLogs.LogDate=PMDD.LogDate-Chad |
 |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2012-08-23 : 16:21:37
|
When you use the non-outer table in the WHERE clause, it effectively (mathematically) makes it an INNER JOIN. Perhaps you want to add the WHERE logic to the ON logic:[CODE]select #PMDailyLogs.LogDatefrom #PMDailyLogsLEFT OUTER JOIN PMDD on PMDD.LogDate=#PMDailyLogs.LogDate and PMDD.Project='120127.' and PMDD.PMCo=1order by #PMDailyLogs.LogDate[/CODE]=================================================We are far more concerned about the desecration of the flag than we are about the desecration of our land. -Wendell Berry |
 |
|
ATG
Starting Member
35 Posts |
Posted - 2012-08-23 : 16:34:39
|
Thanks Bustaz. That worked. That seems odd though. I feel like I've done a million OUTER joins and have never encountered this issue. Could it be something to do with it accessing a temp table? |
 |
|
ATG
Starting Member
35 Posts |
Posted - 2012-08-23 : 16:48:28
|
Nevermind. I see what you mean. At one point i had tried adding(PMDD.LogDate is null or not(PMDD.LogDate is null))which I thought would get around that, but it didn't :( |
 |
|
|
|
|