Something like this:
Declare @factTable Table (Account varchar(20));
Insert Into @factTable (Account)
Values ('Account1'), ('Account2'), ('Account3');
Set Nocount On;
Declare @sqlCommand nvarchar(max) = ''
, @columnList nvarchar(max) = ''
, @pivotColumns nvarchar(max) = '';
Select @columnList = (Select Distinct concat(', ', quotename(ft.Account), ' = ', 'coalesce(', quotename(ft.Account), ', 0)')
From @factTable ft
For xml Path(''), Type).value('.', 'nvarchar(max)');
Select @pivotColumns = stuff((Select Distinct concat(', ', quotename(ft.Account))
From @factTable ft
For xml Path(''), Type).value('.', 'nvarchar(max)'), 1, 2, '');
Set @sqlCommand = '
Use SomeDatabase;
Go
Drop View If Exists dbo.Pivoted_Fact;
Go
Create View dbo.Pivoted_Fact
As
Select ft.Period
, ft.Scenario
, Styling
--, Country_Style_Code
, ' + @columnList + '
From @factTable ft
Pivot (sum([Vals]) FOR [Accounts] IN (' + @pivotColumns + ')) As pf;';
Print @sqlCommand;
--Execute sp_executesql @sqlCommand;