
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE [dbo].[p_grid_act_cies]
	@company_id as varchar(15) = '%',
	@family_code as ais_code_short = '%'
AS

/*
	@company_id - camp de filtrare
	@family_code
		- '%' - nu se tine cont de acest camp, query-ul intoarce toate companiile (filtreaza dupa @company_id)
		- != '%' - sunt returnate companiile apartinand familiei specificate (tabela pr_families_cies), cu filtru pe @company_id
*/


set nocount on

declare @id int 

if @company_id = '%'
	set @id = 0
else
	set @id = convert(int, @company_id)


select	*
from	vw_act_cies
where	(@id = 0 or act_actor_id = @id)
and		(
			(@family_code = '%')
			
			OR
			
			EXISTS	(
					SELECT	1
					FROM	pr_families_cies
					WHERE	prfc_family_code = @family_code
					AND		prfc_company_id = cie_actor_id )
		)
GO
