Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

This Stored Procedure is causing a deadlocks , Please advise

$
0
0
USE [myNIC_PROD]
GO
/****** Object:  StoredProcedure [dbo].[NIC_Dashboard_RunRPT]    Script Date: 08/13/2013 19:18:26 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
-- exec [NIC_Dashboard_RunRPT]


ALTER PROCEDURE [dbo].[NIC_Dashboard_RunRPT]	
AS
BEGIN
	Print '1) Dashboard Overnight Report - Started'
		DECLARE @dRun datetime
		DECLARE @nCategory int
			
		Print '3) Get The current time'
		Set @dRun = getdate()
		
	Print '4) Get the list of all the PHYSICIANS in the system'	
		Truncate table [NIC_Dashboard_OvernightRPT]

		insert into [NIC_Dashboard_OvernightRPT]
		([lEnterprise],[lOffice],[lPhysician],[dRun],[dSuccessful])
		(
			select o.lEnterprise,map.lOffice,Phys.lid, @dRun,0 
			from Physician phys
				INNER JOIN Map_PhysicianTooffice map ON phys.lID=map.lPhysician
				INNER JOIN Office o ON map.loffice=o.lId
			where Phys.nRecordStatus = 1
				AND o.nRecordStatus=1
				AND map.nRecordStatus=1	
		)	

	
	Print '5) Get count for the Clinical Report'

		Truncate table [NIC_Dashboard_OvernightRPT_ClinicalStat_Summary]
	
		-- NIC_OA_HM_GetOvernightProfilesForPhysician

		Print '5.1) HM Reports : Immunization'			
		--	exec NIC_Dashboard_Immunization_Insert
		SET @nCategory = 1
		INSERT INTO [NIC_Dashboard_OvernightRPT_ClinicalStat_Summary]
		([lRPT],[lJobType],[nCount])
		(
		             SELECT RPT.lid, @nCategory ,isNull(Count(opp.lid),0)
		             FROM HMOvernightProfilePatients opp  
			INNER JOIN RosteredPatient r on r.lPatient = opp.lPatient and opp.lPhysician=r.lPhysician -- Add by David at 2008-09-11
		             INNER JOIN Patient pat ON pat.lID = opp.lPatient
		             INNER JOIN [NIC_Dashboard_OvernightRPT] RPT on RPT.lPhysician = opp.lPhysician
		             INNER JOIN HMProfile HM ON HM.lID = opp.lProfile and HM.nRecordStatus=1
		             WHERE HM.nCategory = @nCategory 
				And opp.nResponse = 0
				And r.dEndRoster>=@dRun -- Add by David at 2008-09-11
				And pat.nStatus not in (1,2) -- Add by David at 2008-09-11
		             GROUP BY RPT.lid
		)

		Print '5.2) HM Reports : Influenza'
		--	exec NIC_Dashboard_Influenza_Insert
		SET @nCategory = 2
		INSERT INTO [NIC_Dashboard_OvernightRPT_ClinicalStat_Summary]
		([lRPT],[lJobType],[nCount])
		(
             SELECT RPT.lid, @nCategory ,isNull(Count(opp.lid),0)
             FROM HMOvernightProfilePatients opp  
             INNER JOIN Patient pat ON pat.lID = opp.lPatient
             INNER JOIN [NIC_Dashboard_OvernightRPT] RPT on RPT.lPhysician = opp.lPhysician
             INNER JOIN HMProfile HM ON HM.lID = opp.lProfile and HM.nRecordStatus=1
             WHERE HM.nCategory = @nCategory
			 And opp.nResponse = 0  
             GROUP BY RPT.lid
		)
		Print '5.3) HM Reports : Mammogram'
		--	exec NIC_Dashboard_Mammogram_Insert
		SET @nCategory = 3
		INSERT INTO [NIC_Dashboard_OvernightRPT_ClinicalStat_Summary]
		([lRPT],[lJobType],[nCount])
		(
             SELECT RPT.lid, @nCategory ,isNull(Count(opp.lid),0)
             FROM HMOvernightProfilePatients opp  
             INNER JOIN Patient pat ON pat.lID = opp.lPatient
             INNER JOIN [NIC_Dashboard_OvernightRPT] RPT on RPT.lPhysician = opp.lPhysician
             INNER JOIN HMProfile HM ON HM.lID = opp.lProfile and HM.nRecordStatus=1
             WHERE HM.nCategory = @nCategory
			And opp.nResponse = 0  
			And pat.nStatus not in (1,2) -- Add by David Jin at 2008-09-11
			And opp.lpatient not in (SELECT chp.lpatient  -- Add by David Jin at 2008-09-11
             			 	FROM chhospitalizations chp  -- Add by David Jin at 2008-09-11
			             WHERE  chp.lpatient=opp.lpatient and (chp.lprocedureitem=250000)) -- Add by David Jin at 2008-09-11
             GROUP BY RPT.lid
		)
		Print '5.4) HM Reports : PapSmear'
		--	exec NIC_Dashboard_PapSmear_Insert
		SET @nCategory = 4
		INSERT INTO [NIC_Dashboard_OvernightRPT_ClinicalStat_Summary]
		([lRPT],[lJobType],[nCount])
		(
             SELECT RPT.lid, @nCategory ,isNull(Count(opp.lid),0)
             FROM HMOvernightProfilePatients opp  
             INNER JOIN Patient pat ON pat.lID = opp.lPatient
             INNER JOIN [NIC_Dashboard_OvernightRPT] RPT on RPT.lPhysician = opp.lPhysician
             INNER JOIN HMProfile HM ON HM.lID = opp.lProfile and HM.nRecordStatus=1
             WHERE HM.nCategory = @nCategory
			 And opp.nResponse = 0  
  			And pat.nStatus not in (1,2) -- Add by David Jin at 2008-09-11
			And opp.lpatient not in (SELECT chp.lpatient  -- Add by David Jin at 2008-09-11
             			 	FROM chhospitalizations chp  -- Add by David Jin at 2008-09-11
			             WHERE  chp.lpatient=opp.lpatient and (chp.lprocedureitem=250001)) -- Add by David Jin at 2008-09-11
           GROUP BY RPT.lid
		)
---- begin		
	Print '5.5) HM Reports : FOBT'
		--	exec NIC_Dashboard_PapSmear_Insert
		SET @nCategory = 7   --  5 was used in the  QueueSettings table lid as Recall, even in HM it is 5
  
		INSERT INTO [NIC_Dashboard_OvernightRPT_ClinicalStat_Summary]
		([lRPT],[lJobType],[nCount])
		(
             SELECT RPT.lid, @nCategory ,isNull(Count(opp.lid),0)
             FROM HMOvernightProfilePatients opp  
             INNER JOIN Patient pat ON pat.lID = opp.lPatient
             INNER JOIN [NIC_Dashboard_OvernightRPT] RPT on RPT.lPhysician = opp.lPhysician
             INNER JOIN HMProfile HM ON HM.lID = opp.lProfile and HM.nRecordStatus=1
             WHERE HM.nCategory = 5
			 And opp.nResponse = 0  
             GROUP BY RPT.lid
		)

--end
		Print '5.5) Recall'  -- nCategory : 1
			exec NIC_Dashboard_Recalls_Insert 1
		Print '5.6) Follow Up'  -- nCategory : 4
			exec NIC_Dashboard_Recalls_Insert 4


	Print '6) Generate Operational Report'
		Truncate table [NIC_Dashboard_OperationalStat_Summary]
		exec NIC_Dashboard_GetOperationalStat

	Print '7) Optional RPT'
		TRUNCATE TABLE NIC_Dashboard_OptionalChart_Summary
		exec NIC_Dashboard_InsertOptionalChartData @dRun 
	

	Print '8) Optional RPT : Patient Trents' -- Patient Trends ( Scheduled vs Seen )
		TRUNCATE TABLE NIC_Dashboard_PatientTrends
		exec NIC_Dashboard_AppointmentRPT
	
	Print '9) Generate Financial Report'
		Truncate table [NIC_Dashboard_FinancialStat_Summary]

		exec NIC_Dashboard_FinancialStat_GetPayments
		exec NIC_Dashboard_FinancialStat_GetAdjustments
		exec NIC_Dashboard_FinancialStat_GetARs
		exec NIC_Dashboard_FinancialStat_GetCharges

	Print '10) mark RPT as successful'
		UPDATE NIC_Dashboard_OvernightRPT Set dSuccessful = 1

	Print 'Dashboard Overnight Report - Completed'
END


k


Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>