2

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
1
  • Place double apostrophes by won, lost and open constants. Commented Aug 31, 2016 at 5:26

1 Answer 1

2

change below part in your code

  iif (Stage_Edited = ''won'', opp_id,null) WonOpps,
                    iif (Stage_Edited = ''lost'', opp_id,null) LostOpps,
                    iif (Stage_Edited = ''open'', opp_id,null) OpenOpps
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks :) That Worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.