Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Using WHERE Not Exists for a Fact Table Load

$
0
0
I have a stored procedure in that attempts to perform a WHERE NOT EXISTS check to insert new records. If the table is empty, the procedure will load the table. However, an insert does not occur when a change to one or more source fields occurs against an existing record. The following is my code:declare @Created_By nchar(50),@Created_Date datetime,@Updated_By nchar(50),@Updated_Date datetimeselect @Created_By = system_user,@Created_Date = getdate(),@Updated_By = system_user,@Updated_Date = getdate()insert fact.Appointment(Slot_ID, Slot_DateTime, Slot_StartDateTime, Slot_EndDateTime, Slot_Duration_min, Slot_CreateDateTime, Slot_CreateDate_DateKey, Healthcare_System_ID, Healthcare_Service_ID, Healthcare_Supervising_Service_ID, Healthcare_Site_ID, Booked_Appt_ID, Appt_Notification_Submission_DateKey, Appt_Notification_Completion_DateKey, Appt_Notification_Duration, Appt_Notification_ID, Patient_ID, Physician_ID, Referral_ID, Specialty, LanguageRequested, Created_Date, Created_By, Updated_Date, Updated_By)select distinctSlot.Slot_ID , Slot.Slot_Start_DateTime as Slot_DateTime --???, Slot.Slot_Start_DateTime, Slot.Slot_End_DateTime, datediff(mi,slot.Slot_Start_DateTime,slot.Slot_End_Datetime) as Slot_Duration_Min , Slot.Created_Date as Slot_CreateDateTime, SlotCreateDate.Date_key as Slot_CreateDate_DateKey, HSite.Healthcare_System_ID, HSite.Healthcare_Service_ID, HSite.Healthcare_Service_ID as Healthcare_Supervising_Service_ID, HSite.Healthcare_Site_ID , Ref.Booked_Appt_ID , ApptSubmissionTime.Date_key as Appt_Notification_Submission_DateKey, ApptCompletionTime.Date_key as Appt_Notification_Completion_DateKey, datediff(mi,appt.SubmissionTime,appt.CompletionTime) as Appt_Notification_Duration, Appt.Appt_Notification_ID , pat.Patient_ID , 0 as Physician_ID, ref.Referral_ID, Hsrv.Specialty, appt.[Language] as LanguageRequested,@Created_Date as Created_Date,@Created_By as Created_By,@Updated_Date as Updated_Date,@Updated_By as Updated_Byfrom dim.Healthcare_System HSysinner join dim.Healthcare_Service HSrvon HSys.Healthcare_System_ID = HSrv.HealthCare_System_ID inner join dim.Healthcare_Site HSiteon HSite.HealthCare_Service_ID = HSrv.Healthcare_Service_IDand HSite.HealthCare_System_ID = HSrv.HealthCare_System_ID inner join dim.Referral Ref on Ref.ReferralSite_ID = HSite.Site_IDand Ref.ReferralService_ID = HSite.Service_IDand Ref.ReferralSystem_ID = HSite.System_ID right join (select distinct Slot_ID, Source_Slot_ID, Slot_Start_DateTime, Slot_End_DateTime, Created_Date from dim.slot)sloton ref.Source_Slot_ID = slot.Source_Slot_IDinner join dim.Appointment_Notification appton appt.System_ID = HSys.System_IDinner join dim.Patient pat on pat.Source_Patient_ID = appt.Source_Patient_IDinner join dim.SystemUser SysUseron SysUser.Healthcare_System_ID = HSys.Healthcare_System_IDleft join dim.Calendar SlotCreateDateon SlotCreateDate.Full_DateTime = cast(Slot.Created_Date as smalldatetime)left join dim.Calendar ApptSubmissionTimeon ApptSubmissionTime.Full_DateTime = cast(appt.SubmissionTime as smalldatetime)left join dim.Calendar ApptCompletionTimeon ApptCompletionTime.Full_DateTime = cast(appt.CompletionTime as smalldatetime)where not exists (selectSlot_ID, Slot_DateTime, Slot_StartDateTime, Slot_EndDateTime, Slot_Duration_min, Slot_CreateDateTime, Slot_CreateDate_DateKey, Healthcare_System_ID, Healthcare_Service_ID, Healthcare_Supervising_Service_ID, Healthcare_Site_ID, Booked_Appt_ID, Appt_Notification_Submission_DateKey, Appt_Notification_Completion_DateKey, Appt_Notification_Duration, Appt_Notification_ID, Patient_ID, Physician_ID, Referral_ID, Specialty, LanguageRequested, Created_Date, Created_By, Updated_Date, Updated_Byfrom fact.Appointment factwhere (Slot.Slot_ID = fact.Slot_ID orSlot.Slot_Start_DateTime = fact.Slot_DateTime orSlot.Slot_Start_DateTime = fact.Slot_StartDateTimeorSlot.Slot_End_DateTime = fact.Slot_EndDateTimeordatediff(mi,slot.Slot_Start_DateTime,slot.Slot_End_Datetime) =fact.Slot_Duration_minorSlot.Created_Date = fact.Slot_CreateDateTimeorSlotCreateDate.Date_key = fact.Slot_CreateDate_DateKeyorHSite.Healthcare_System_ID = fact.Healthcare_System_IDorHSite.Healthcare_Service_ID = fact.Healthcare_Service_IDorHSite.Healthcare_Service_ID = fact.Healthcare_Service_ID orHSite.Healthcare_Site_ID = fact.Healthcare_Site_ID orRef.Booked_Appt_ID = fact.Booked_Appt_ID orApptSubmissionTime.Date_key =fact.Appt_Notification_Submission_DateKeyorApptCompletionTime.Date_key =fact.Appt_Notification_Completion_DateKeyor datediff(mi,appt.SubmissionTime,appt.CompletionTime) = fact.Appt_Notification_DurationorAppt.Appt_Notification_ID = fact.Appt_Notification_ID orpat.Patient_ID = fact.Patient_ID or0 = 0orref.Referral_ID = fact.Referral_IDorHsrv.Specialty = fact.Specialtyorappt.[Language] = fact.LanguageRequested))--*********************I expected that when one of the source values of any field in the second WHERE clause changes, that the procedure would insert a new record. Why is this not happening? One other note: I am not 'allowed' to use MERGE.Thank you for your help.CSDunn

Viewing all articles
Browse latest Browse all 3145

Trending Articles