SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
create procedure [dbo].[p_fields_linked_tables_values]
	@fields_names_codes type_table_name_field_name_field_value READONLY

AS

set nocount on;

select	mfc.fld_table_name, 
		mfc.fld_field_name,
		fnc.field_value,  
		lval_link_table_name = mlv.lval_link_table_name,
		lval_l1 = case when mlv.lval_link_table_name is not null then mlv.lval_l1 else fnc.field_value end, --daca nu gasim label-ul punem codul
		lval_l2 = mlv.lval_l2,
		lval_l3 = mlv.lval_l3
from @fields_names_codes fnc 
	inner join md_fields_cpl mfc on		fnc.table_name = mfc.fld_table_name
									and fnc.field_name = mfc.fld_field_name
	left join md_link_values mlv on		mlv.lval_link_table_name = mfc.fld_link_table_name
									and mlv.lval_value = fnc.field_value
GO
