SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE procedure [dbo].[p_cpl_table_datafields]
	@table_name ais_name,
	@form_code ais_code_short

as  

	declare @field_prefix varchar(10),
			@available_fields varchar(max)
	
	select  @field_prefix = case @table_name
								when 'act_cpl' then 'acpl_'
								when 'pol_cpl' then 'pcpl_'
								when 'ris_cpl' then 'rcpl_'
								when 'claim_cpl' then 'ccpl_'
								when 'claim_actors_cpl' then 'clactcpl_'
								else ''
							end
	
	select	@available_fields = frm_available_fields
	from	dyn_forms f
	where	f.frm_form_code = @form_code



	select datafield = fld_table_name + '.' + @field_prefix + fld_field_name, fld_l1, fld_l2, fld_l3,
			fld_type,
			fld_link_table_name,
			fld_size
	from	md_fields_cpl
	where	fld_table_name = @table_name 
	and		( 
			ltrim(rtrim(isnull(@available_fields, ''))) = ''
			or
			CHARINDEX(';' + fld_field_name + ';', ';' + @available_fields + ';') > 0
			)
  
 
GO
