This is my code, I am facing trouble in using iff function in the Dynamic sql string The SQL server posting the error as "Incorrect syntax near 'won'." Help me with this.
Declare @Colname NVARCHAR (128)
Declare @SQl NVARCHAR(MAX)
set @Colname = N'Channel_Edited'
Set @SQl = N'Select '+@Colname+', Count(WonOpps) as Won, Count(LostOpps) as Lost, Count(OpenOpps) as [open]
From
( select '+@Colname+',
iif (Stage_Edited = 'won', opp_id,null) WonOpps,
iif (Stage_Edited = 'lost', opp_id,null) LostOpps,
iif (Stage_Edited = 'open', opp_id,null) OpenOpps
From CRM_Deal2
) as ABC
Group by '+@Colname
Exec sp_executesql @SQL