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 |
kshahzad
Starting Member
45 Posts |
Posted - 2013-07-18 : 15:16:30
|
CREATE PROCEDURE dbo.NIC_CPP_LABRESULTS_GetReviewedPatientLabTests_AOS@lPatient int,@nRecordStatusint,@nClassLevel int=5,@nSecurityLevelint=0,@lMap_UserToOfficeint=0ASSET TRANSACTION ISOLATION LEVEL READ COMMITTEDBEGINDeclare @lUser int,@lUserActionMappingTable intselect @luser=lUser from map_userToOffice where lid=@lMap_UserToOfficeset @lUserActionMappingTable=32--Disable the AOS SECURITY FOR CPPif @nSecurityLevel=2 set @nSecurityLevel=1if @lMap_UserToOffice>0 and @nSecurityLevel>=2 -- if there is a user and masking is on(bit 1=enabled masking)begin------------- declare @dCurDate datetime, @lOffset int--set @dCurDate=getDate()-- may need to adjust this for users timezoneexec SESSION_GetUserDate @lUser =@luser ,@dUserDate =@dCurDate OUTPUT,@lOffset=@lOffset OUTPUT--This gets the date in the users timezone/*Get the permission ID for this section....*/declare @lCustodianPermission intselect @lCustodianPermission =lid from Custodian_Permissions where szcode='CPP'declare @PhysiciansAccess table (lPhysician int primary key)insert into @PhysiciansAccess (lPhysician)select m.lPhysician from map_UserToCustodian minner join map_UserToCustodian_permissions cp on cp.lmap_UserToCustodian=m.lid and cp.lCustodianpermission=@lCustodianPermissionwhere m.lUser=@luser and m.nRecordStatus=1 and m.bAllPatient=1 and isNull(m.dStartDate,'1900-01-01')<=@dCurDate and isnull(m.dEndDate,'9000-01-01')>=@dCurDateUnionselect lPhysician from map_UserToCustodian minner join map_UserToCustodian_Per_patient p on p.lmap_UserToCustodian=m.lidinner join map_UserToCustodian_permissions cp on cp.lmap_UserToCustodian_Per_Patient=p.lid and lCustodianpermission=@lCustodianPermissionwhere lUser=@luser and m.nRecordStatus=1 and p.nRecordStatus=1 and bAllPatient=0 and p.lPatient=@lPatientand isNull(p.dStartDate,'1900-01-01')<=@dCurDate and isNull(p.dEndDate,'9000-01-01')>=@dCurDate--only get if the expiry dates are valid--select * from @PhysiciansAccessdeclare @OfficeAccess table (lOffice int primary key)/*go get all offices that the patient is a member offilter out offices that this patient has a custodain and that custodain did not allow access to this userNote: there is no need to filter by section for this query. The Section has been filtered above, and is used in the @PhysiciansAccess listNOTE: the default values for office is -1 if the information was recorded without an office. This means that it is always accessable.*/ insert into @OfficeAccess (lOffice) values(0)insert into @OfficeAccess (lOffice) select map.loffice from map_PatientToOffice mapinner join map_PatientAtOfficeToCustodian mapc on mapc.lpatient=map.lpatient and mapc.loffice=map.lofficeinner join @PhysiciansAccess PA on PA.lphysician=mapc.lPhysicianwhere map.lpatient=@lPatient--and PA.lPhysician is not nullunion select map.loffice from map_PatientToOffice map left join map_PatientAtOfficeToCustodian mapc on mapc.lpatient=map.lpatient and mapc.loffice=map.loffice where map.lpatient=@lPatient and isNull(mapc.lPhysician,0) <=0-----------SELECT a.*, b.szDescription, p.dDateOrdered, p.dDateSignedOff, p.szLaboratory, p.dDateCollected,Case when exists (select * from mask_role inner join map_UserAtOfficeToRole mapRole on mapRole.lUserRole=mask_Role.lRoleinner join map_userTOoffice map on map.lid=mapRole.lMap_userToOfficewhere mask_role.nMappingTable=@lUserActionMappingTableand map.lUser=@luserand mask_role.lRowID=p.lidand mask_role.nMaskStatus=1)THEN 1WHen Mu.lid is not nullTHEN 1else 0ENDas bMaskFROM [PhysicianLabReportsTests] a JOIN [PhysicianLabReports] p ON p.lID=a.lLabReportJOIN [LaboratoryTest] b ON b.lID=a.lPhysicianLabTestleft Join Mask_User MU on MU.nMappingTable=@lUserActionMappingTable and MU.lUser=@luser and MU.lRowID=p.lid and MU.nMaskStatus=1left join @PhysiciansAccess PA on (PA.lPhysician=p.lPhysician) --LEFT Join for all the physicains the user has access to ALL patient data recorded under that physicianleft join @OfficeAccess O on O.loffice=p.loffice--LEFT Join for all the offices the user has access to ALL patient data in that officeWHERE (p.lPatient=@lPatientAND p.bSignedOff=1AND p.szFiledBy<>'auto'AND a.nRecordStatus=@nRecordStatusand p.nclassificationlevel>=@nClassLevelAND DateDiff(d, p.dDateCollected, (SELECT Max(y.dDateCollected)FROM [PhysicianLabReports] yJOIN [PhysicianLabReportsTests] z ON z.lLabReport=y.lIDWHERE y.lPatient=@lPatientAND z.lPhysicianLabTest=a.lPhysicianLabTestAND z.nRecordStatus=@nRecordStatusAND y.bSignedOff=1)) = 0)or (p.lPatient=@lPatient-- AND p.bSignedOff=1AND p.szFiledBy='auto'AND a.bAdd2CPP = 1AND a.nRecordStatus=@nRecordStatusand p.nclassificationlevel>=@nClassLevelAND DateDiff(d, p.dDateCollected, (SELECT Max(y.dDateCollected)FROM [PhysicianLabReports] yJOIN [PhysicianLabReportsTests] z ON z.lLabReport=y.lIDWHERE y.lPatient=@lPatientAND z.lPhysicianLabTest=a.lPhysicianLabTestAND z.nRecordStatus=@nRecordStatusand Z.bAdd2CPP = 1-- AND y.bSignedOff=1)) = 0)and isnull(PA.lPhysician,O.lOffice) is not null--Only return records if EITHER of the left joins returned a matching recordORDER BY b.szDescriptionendelse if @lMap_UserToOffice>0 and @nSecurityLevel>=1 -- if there is a user and masking is on(bit 1=enabled masking)beginSELECT a.*, b.szDescription, p.dDateOrdered, p.dDateSignedOff, p.szLaboratory, p.dDateCollected,Case when exists (select * from mask_role inner join map_UserAtOfficeToRole mapRole on mapRole.lUserRole=mask_Role.lRoleinner join map_userTOoffice map on map.lid=mapRole.lMap_userToOfficewhere mask_role.nMappingTable=@lUserActionMappingTableand map.lUser=@luserand mask_role.lRowID=p.lidand mask_role.nMaskStatus=1)THEN 1WHen Mu.lid is not nullTHEN 1else 0ENDas bMaskFROM [PhysicianLabReportsTests] a JOIN [PhysicianLabReports] p ON p.lID=a.lLabReportJOIN [LaboratoryTest] b ON b.lID=a.lPhysicianLabTestleft Join Mask_User MU on MU.nMappingTable=@lUserActionMappingTable and MU.lUser=@luser and MU.lRowID=p.lid and MU.nMaskStatus=1WHERE (p.lPatient=@lPatientAND p.bSignedOff=1AND p.szFiledBy<>'auto'AND a.nRecordStatus=@nRecordStatusand p.nclassificationlevel>=@nClassLevelAND DateDiff(d, p.dDateCollected, (SELECT Max(y.dDateCollected)FROM [PhysicianLabReports] yJOIN [PhysicianLabReportsTests] z ON z.lLabReport=y.lIDWHERE y.lPatient=@lPatientAND z.lPhysicianLabTest=a.lPhysicianLabTestAND z.nRecordStatus=@nRecordStatusAND y.bSignedOff=1)) = 0)or (p.lPatient=@lPatient-- AND p.bSignedOff=1AND p.szFiledBy='auto'AND a.bAdd2CPP = 1AND a.nRecordStatus=@nRecordStatusand p.nclassificationlevel>=@nClassLevelAND DateDiff(d, p.dDateCollected, (SELECT Max(y.dDateCollected)FROM [PhysicianLabReports] yJOIN [PhysicianLabReportsTests] z ON z.lLabReport=y.lIDWHERE y.lPatient=@lPatientAND z.lPhysicianLabTest=a.lPhysicianLabTestAND z.nRecordStatus=@nRecordStatusand Z.bAdd2CPP = 1-- AND y.bSignedOff=1)) = 0)ORDER BY b.szDescriptionendelsebeginSELECT a.*, b.szDescription, p.dDateOrdered, p.dDateSignedOff, p.szLaboratory,0 as bMask, p.dDateCollectedFROM [PhysicianLabReportsTests] a JOIN [PhysicianLabReports] p ON p.lID=a.lLabReportJOIN [LaboratoryTest] b ON b.lID=a.lPhysicianLabTestWHERE (p.lPatient=@lPatientAND p.bSignedOff=1AND p.szFiledBy<>'auto'AND a.nRecordStatus=@nRecordStatusAND DateDiff(d, p.dDateCollected, (SELECT Max(y.dDateCollected)FROM [PhysicianLabReports] yJOIN [PhysicianLabReportsTests] z ON z.lLabReport=y.lIDWHERE y.lPatient=@lPatientAND z.lPhysicianLabTest=a.lPhysicianLabTestAND z.nRecordStatus=@nRecordStatusAND y.bSignedOff=1)) = 0)or (p.lPatient=@lPatient-- AND p.bSignedOff=1AND p.szFiledBy='auto'AND a.bAdd2CPP = 1AND a.nRecordStatus=@nRecordStatusAND DateDiff(d, p.dDateCollected, (SELECT Max(y.dDateCollected)FROM [PhysicianLabReports] yJOIN [PhysicianLabReportsTests] z ON z.lLabReport=y.lIDWHERE y.lPatient=@lPatientAND z.lPhysicianLabTest=a.lPhysicianLabTestAND z.nRecordStatus=@nRecordStatusand Z.bAdd2CPP = 1-- AND y.bSignedOff=1)) = 0)ORDER BY b.szDescriptionendENDGO |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
kshahzad
Starting Member
45 Posts |
Posted - 2013-07-18 : 15:55:57
|
IO are slow and since this SP is runnng on Microsoft SQL Enterprise Manager Version: 8.0, so i cant collect execution plan. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
kshahzad
Starting Member
45 Posts |
Posted - 2013-07-19 : 13:28:18
|
Sorry i could not figure out how to collect execution plan of a stored procedure on a run time, |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|
|
|