drop procedure if exists dbo.sp_SetParentsXRay;
CREATE PROCEDURE dbo.sp_SetParentsXRay()
as $$
	begin
update AKUZ.T_X_RAY_DIAGNOSTICS set Parent = dbo.fn_GetParentXRay(MedicalRecordID)
from AKUZ.T_X_RAY_DIAGNOSTICS t0
 where not exists (select 1 from AKUZ.T_REGISTER_ITEM where service = t0.MedicalRecordID limit 1)
 and  not exists (select 1 from AKUZ.T_REGISTER_ITEM where service = t0.Parent limit 1);
	end; 
$$  LANGUAGE plpgsql;