SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE procedure [dbo].[p_policies_get_last_situation_info]
	@policies_ids type_table_id readonly,
	@return_situation_data bit

as
	set nocount on;

	if @return_situation_data = 0
	begin

		select	pol_policy_id,			
				pol_situations_count,			
				pol_last_policy_situation_id,	
				pol_age,
				pol_start_date
		from pol_policies(nolock) pp
			inner join @policies_ids pi on pi.id = pp.pol_policy_id

	end
	else
	begin
		select	pol_policy_id,			
				pol_situations_count,			
				pol_last_policy_situation_id,	
				pol_age,
				pol_start_date,
				pps.pols_mvt_code,
				pps.pols_mvt_date,
				pps.pols_incomplete,
				pps.pols_number,
				pps.pols_sequence_code
		from pol_policies(nolock) pp
			inner join @policies_ids pi on pi.id = pp.pol_policy_id
			inner join pol_policies_situations(nolock) pps on pps.pols_policy_situation_id = pp.pol_last_policy_situation_id
	end

GO
