/*<OXADWDB>*/
/*Add col to BDD_OBJECTS*/
if not exists (select * from <OXASQLDB_DATABASE>.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='BDD_OBJECTS' and COLUMN_NAME='BDD_TABLE_EN') alter table <PREFIXE_HA>BDD_OBJECTS add BDD_TABLE_EN varchar(100) default '' 
if not exists (select * from <OXASQLDB_DATABASE>.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='BDD_OBJECTS' and COLUMN_NAME='BDD_COL_EN') alter table <PREFIXE_HA>BDD_OBJECTS add BDD_COL_EN   varchar(100) default ''

/*<SEPARATEUR>*/
/*Update col BDD_OBJECTS EN*/
IF OBJECT_ID(N'<PREFIXE_HA>TEMP_OXAETL_TEMP_JPM', N'U') IS NOT NULL 
BEGIN
update b set BDD_TABLE_EN=t.NOM_PAR_EN, BDD_COL_EN=t.DB_NAME_EN
    from <PREFIXE_HA>BDD_OBJECTS b, <PREFIXE_HA>TEMP_OXAETL_TEMP_JPM t where b.BDD_TABLE=t.NOM_PAR and b.BDD_COL=t.DB_NAME
END

/*<SEPARATEUR>*/ 
DROP TABLE IF EXISTS  <PREFIXE_HA>TEMP_OXAETL_VIEWS_TRADS
    
/*<SEPARATEUR>*/
/*Update col BDD_OBJECTS EN*/
SELECT 'create or alter view '+ max(BDD_TABLE_EN) 	+' as (select ' as VIEW_NAME, 
    STRING_AGG(replace(BDD_COL,' ','') +' as '+BDD_COL_EN , ' ,')
		
	WITHIN GROUP (ORDER BY o.BDD_ORDRE asc) as COLS,
	' from '+ max( replace(BDD_TABLE,' ','')  )+')' as TABLE_NAME
	into <PREFIXE_HA>TEMP_OXAETL_VIEWS_TRADS
FROM     <PREFIXE_HA>BDD_OBJECTS o
WHERE    BDD_TABLE like ('%') 
and isnull(BDD_COL,'')<>'' and isnull(BDD_TABLE_EN,'')<>'' 
group by BDD_TABLE_EN

    
/*<SEPARATEUR>*/    
DECLARE @Sql NVARCHAR(MAX);
DECLARE Cur CURSOR LOCAL FAST_FORWARD FOR 
    (SELECT VIEW_NAME+COLS+TABLE_NAME      FROM <PREFIXE_HA>TEMP_OXAETL_VIEWS_TRADS  )

OPEN Cur
FETCH NEXT FROM Cur INTO @Sql

WHILE (@@FETCH_STATUS = 0)
BEGIN
    --Exec sp_executesql @Sql
	print @Sql
    EXEC ( @Sql )
    FETCH NEXT FROM Cur INTO @Sql 
END
CLOSE Cur
DEALLOCATE Cur;     