0

I have a small question

 @CMD nvarchar (MAX)='' 
SELECT @CMD +=N'


 SELECT
     COUNT(CASE WHEN RFRD.QueuTypeID=4
           THEN RFRD.QueuTypeID
           END) RequiredaQueue
    ,COUNT(CASE WHEN RFRD.QueuTypeID=3
          THEN RFRD.QueuTypeID
          END) BhinaQueue      
    ,COUNT(CASE WHEN RFRD.QueuTypeID NOT IN (3,4) 
    ,COUNT(DISTINCT RFRD.LeadDocumentID)AS Repeat,CONCAT('''+RF+''' ,''*'')A'                                              
FROM #cte RF 

p.s - The From is outside the dynamic.

Works fine here... But when I add IF statement:

   SELECT
     COUNT(CASE WHEN RFRD.QueuTypeID=4
           THEN RFRD.QueuTypeID
           END) RequiredaQueue
    ,COUNT(CASE WHEN RFRD.QueuTypeID=3
          THEN RFRD.QueuTypeID
          END) BhinaQueue      
    ,COUNT(CASE WHEN RFRD.QueuTypeID NOT IN (3,4)'
     **IF @MyParam IS NOT NULL**
   SET @CMD+='
    ,COUNT(DISTINCT RFRD.LeadDocumentID)AS Repeat,CONCAT('''+RF+'''                                               ,''*'')A'                                              
FROM #cte RF 

The program doesn't understand what is the RF means. it doesn't see the FROM #cte RF Anymore. any body knows whats the problem with the IF statement?? thanks

7
  • 1
    you forgot then in if statement Commented Jun 24, 2015 at 9:21
  • 1
    @AmeyaDeshpande, it is not necessary Commented Jun 24, 2015 at 9:23
  • @GiorgiNakeuri Thanks dont know about it. Commented Jun 24, 2015 at 9:24
  • 1
    You have some messed up code. What are you trying to do? Commented Jun 24, 2015 at 9:25
  • 4
    Seems like an XY problem to me. What exactly are you trying to acheive? Commented Jun 24, 2015 at 9:30

1 Answer 1

1

Then don't use if. Use case:

 @CMD nvarchar (MAX)='' 

SELECT @CMD +=N'
 SELECT
     COUNT(CASE WHEN RFRD.QueuTypeID=4
           THEN RFRD.QueuTypeID
           END) as RequiredaQueue
    ,COUNT(CASE WHEN RFRD.QueuTypeID=3
          THEN RFRD.QueuTypeID
          END) as BhinaQueue   
    ,COUNT(CASE WHEN RFRD.QueuTypeID NOT IN (3,4) '
    (case when @MyParem is not null then ',COUNT(DISTINCT RFRD.LeadDocumentID)AS Repeat,CONCAT('''+RF+''' ,''*'')A' 
          else ''
     end)  + '                                           
FROM #cte RF ';

All the code is inside a select statement, so case is the appropriate construction, not if.

Sign up to request clarification or add additional context in comments.

Comments

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.