ALTER PROCEDURE [dbo].[NIC_OA_HM_FullTextSearch] @lEnterprise INT, @szSearch VARCHAR(1000), @dStart DATETIME = NULL, @dEnd DATETIME = NULL, @lOffice INT = NULL, @lPhysician INT = NULL, @szSection VARCHAR(100) = 'All' AS SET TRANSACTION ISOLATION LEVEL READ COMMITTED IF @dStart IS NULL BEGIN SET @dStart = '1900-01-01' END IF @dEnd IS NULL BEGIN SET @dEnd = '9000-01-01' END IF @lPhysician = 0 BEGIN SET @lPhysician = NULL END IF @szSection = 'All' BEGIN SET @szSection = NULL END SET @szSearch = '%' + @szSearch + '%' DECLARE @Search TABLE ( lPatient INT, dDate DATETIME, lID INT, szType VARCHAR(200), szData NTEXT ) INSERT INTO @Search ( lPatient, dDate, lID, szType, szData ) --Patient Notes SELECT lPatient, dDate, lID, szType, szData FROM ( SELECT lID AS lPatient, dlastUpdated AS dDate, lID AS lID, 'Patient Notes' AS szType, szNotes szData FROM Patient WHERE lEnterprise = @lEnterprise AND szNotes LIKE @szSearch AND lPhysicianResponsible = COALESCE(@lPhysician, lPhysicianResponsible) UNION ALL --Patient Alerts SELECT lPatient, pat.dlastUpdated AS dDate, PA.lID, 'Patient Alerts' AS szType, szAlert AS szData FROM PatientAlert PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szAlert LIKE @szSearch AND lPhysicianResponsible = COALESCE(@lPhysician, lPhysicianResponsible) UNION ALL --Allergy Comments SELECT lPatient, dLastModified AS dDate, PA.lID, 'Allergy Comments' AS szType, szComment AS szData FROM CHAllergy PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szComment LIKE @szSearch AND dLastModified BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Past Medical History SELECT lPatient, PA.dLastModified AS dDate, PA.lID, 'Past Medical History' AS szType, szMedicalHistory AS szData FROM CHMedicalHistory PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND szMedicalHistory LIKE @szSearch AND dLastModified BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Medical Alerts SELECT lPatient, PA.dlastModified dDate, PA.lID, 'Medical Alert' AS szType, szAlert AS szData FROM CHAlert PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND szAlert LIKE @szSearch AND dLastModified BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Immunizations Comments SELECT lPatient, dLastModified AS dDate, PA.lID, 'Immunizations Comments' AS szType, szComment AS szData FROM CHPreMASTImmunizations PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szComment LIKE @szSearch AND dLastModified BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Injections Comments SELECT lPatient, dLastModified dDate, PA.lID, 'Injections Comments' AS szType, szComments szData FROM CHPreMASTInjections PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szComments LIKE @szSearch AND dLastModified BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Problem List Comments SELECT lPatient, PA.dLastUpdated AS dDate, PA.lID, 'Problem List Comments' asType, szComments szData FROM CHProblemList PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szComments LIKE @szSearch AND PA.dLastUpdated BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Procedures Comments SELECT lPatient, dLastModified AS dDate, PA.lID, 'Procedures Comments' AS szType, szComments AS szData FROM CHHospitalizations PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szComments LIKE @szSearch AND dLastModified BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Social History Comments SELECT lPatient, dLastModified AS dDate, PA.lID, 'Social History Comments' AS szType, szComments AS szData FROM CHSocialHistory PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szComments LIKE @szSearch AND dLastModified BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Family History Comments SELECT lPatient, dLastModified AS dDate, PA.lID, 'Family History Comments' AS szType, szOther szData FROM CHFamilyHistory PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szOther LIKE @szSearch AND dLastModified BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Rx Instructions SELECT PA.lPatient, presc.dFillDate AS dDate, PA.lID, 'Rx Patient Instructions' AS szType, meddir.szInstructionOther AS szData FROM Medication PA INNER JOIN Patient pat ON PA.lPatient = pat.lID INNER JOIN MedicationDirection meddir ON PA.lID = meddir.lMedication INNER JOIN Map_MedicationToPrescription map ON PA.lID = map.lMedication AND meddir.lID = map.lMedicationDirection INNER JOIN Prescription presc ON map.lPrescription = presc.lID WHERE lEnterprise = @lEnterprise AND presc.nRecordStatus = 1 AND meddir.szInstructionOther LIKE @szSearch AND presc.dCreated BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Rx Pharmacist Instructions SELECT PA.lPatient, presc.dFillDate AS dDate, PA.lID, 'Rx Pharmacist Instructions' szType, meddir.szInstructionPhar AS szData FROM Medication PA INNER JOIN Patient pat ON PA.lPatient = pat.lID INNER JOIN MedicationDirection meddir ON PA.lID = meddir.lMedication INNER JOIN Map_MedicationToPrescription map ON PA.lID = map.lMedication AND meddir.lID = map.lMedicationDirection INNER JOIN Prescription presc ON map.lPrescription = presc.lID WHERE lEnterprise = @lEnterprise AND presc.nRecordStatus = 1 AND meddir.szInstructionPhar LIKE @szSearch AND presc.dCreated BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Rx Notes SELECT PA.lPatient, presc.dFillDate AS dDate, PA.lID, 'Rx Notes' szType, meddir.Annotation AS szData FROM Medication PA INNER JOIN Patient pat ON PA.lPatient = pat.lID INNER JOIN MedicationDirection meddir ON PA.lID = meddir.lMedication INNER JOIN Map_MedicationToPrescription map ON PA.lID = map.lMedication AND meddir.lID = map.lMedicationDirection INNER JOIN Prescription presc ON map.lPrescription = presc.lID WHERE lEnterprise = @lEnterprise AND presc.nRecordStatus = 1 AND meddir.Annotation LIKE @szSearch AND presc.dCreated BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Plan Notes SELECT lPatient, PA.dCurrentDate as dDate, PA.lID, 'Plan Notes' AS szType, szPlanNotes AS szData FROM ProgressNote PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szPlanNotes LIKE @szSearch AND PA.dCurrentDate BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Subjective/Objective Notes SELECT lPatient, PA.dCurrentDate AS dDate, PA.lID, 'Subjective/Objective Notes' as szType, PNCN.szClinicalNotes AS szData FROM ProgressNote PA INNER JOIN Patient pat ON PA.lPatient = pat.lID INNER JOIN ProgressNoteClinicalNote PNCN ON PA.lID = PNCN.lProgressNote WHERE lEnterprise = @lEnterprise AND PA.nRecordStatus = 1 AND PNCN.szClinicalNotes LIKE @szSearch AND PA.dCurrentDate BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Lab Reports Comments SELECT lPatient, PA.dDateFiled AS dDate, PA.lID, 'Lab Reports Comments' AS szType, szComments AS szData FROM PhysicianLabReports PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szComments LIKE @szSearch AND PA.dDateFiled BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --DI Reports Comments SELECT lPatient, PA.dDateFiled AS dDate, PA.lID, 'DI Reports Comments' AS szType, szComments AS szData FROM PhysicianXRayReports PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szComments LIKE @szSearch AND PA.dDateFiled BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Correspondence Reports Comments SELECT lPatient, PA.dDateFiled AS dDate, PA.lID, 'Correspondence Comments' AS szType, szCorrespondence AS szData FROM CorrespondenceReports PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szCorrespondence LIKE @szSearch AND PA.dDateFiled BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Free Form Prescriptions SELECT lPatient, PA.dDatePrescribed AS dDate, PA.lID, 'Free Form Prescriptions' AS szType, szPrescription szData FROM CHPreMASTFreeFormPrescription PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szPrescription LIKE @szSearch AND PA.dDateprescribed BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Free Form Prescriptions SELECT lPatient, PA.dLastModified AS dDate, PA.lID, 'Free Form Prescriptions' AS szType, szPrescription AS szData FROM FreeFormPrescription PA INNER JOIN Patient pat ON PA.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND nRecordStatus = 1 AND szPrescription LIKE @szSearch AND PA.dLastModified BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) UNION ALL --Outgoing Consultation Letter SELECT PN.lPatient, PA.dDateCreated AS dDate, PA.lID, 'Outgoing Consultation Letter' AS szType, 'N/A' AS szData FROM Consultants PA INNER JOIN ConsultationLetter_ToConsultant cons ON PA.lId = cons.lConsultant INNER JOIN map_ConsultationToProgressNote map ON PA.lId = map.lConsultant INNER JOIN ProgressNote PN ON map.lProgressNote = PN.lID INNER JOIN Patient pat ON PN.lPatient = pat.lID WHERE lEnterprise = @lEnterprise AND PA.nRecordStatus = 1 AND cons.stLetter LIKE @szSearch AND PA.dDateCreated BETWEEN @dStart AND @dEnd AND Pat.lPhysicianResponsible = COALESCE(@lPhysician, Pat.lPhysicianResponsible) ) AS dt IF @lOffice <> 0 BEGIN SELECT pat.szLast, pat.szFirst, pat.szChartNum, s.szType, s.dDate, s.szData, s.lID FROM @Search s INNER JOIN Patient pat ON s.lPatient = pat.lID INNER JOIN Map_patientToOffice map ON pat.lID = map.lPatient AND map.lOffice = @lOffice WHERE s.szType = COALESCE(@szSection, s.sztype) ORDER BY pat.szlast, pat.szFirst END ELSE BEGIN SELECT pat.szLast, pat.szFirst, pat.szChartNum, s.szType, s.dDate, s.szData, s.lID FROM @Search s INNER JOIN Patient pat ON s.lPatient = pat.lID WHERE s.szType = COALESCE(@szSection, s.sztype) ORDER BY pat.szlast, pat.szFirst END
k