title: Dynamic sql with CASE expression
body: <pre><code>   SET @SQL =
          'SELECT
          CaseStatus =
              CASE Level1Status
                  WHEN 1100 THEN ''Case Submitted to QC''
                  WHEN 1200 THEN ''Pending QC''
                  WHEN 1400 THEN ''Passed QC''
              
              END,
  </code></pre>
  <p>I'm currently having problems with the dynamic sql/case expression above , as I can't seem to put strings inside dynamic sql, does anyone happen to have a solution to fixing this</p>
  <p><strong>EDIT</strong></p>
  <p>AS
  BEGIN
  DECLARE @SQL nvarchar(4000)</p>
  <pre><code>SET @SQL =
      'SELECT
      CaseStatus =
          CASE Level1Status
              WHEN 1100 THEN ''Case Submitted to QC''
              WHEN 1200 THEN ''Pending QC''
              WHEN 1400 THEN ''Passed QC''
          
          END,
          
          CaseStartDateTime,
          CaseEndDateTime,
          StatusName,
          Cell_NameDescription,
          QCAnalystName,
          AnalystName,
          Upload_Datetime,
          Requesting_Entity,
          Legal_Entity_TypeDescription,
          HighPriorityDescription,
          DD_Level_RequiredDescription,
          CountryDscr,
          Maintable.KYCCaseId AS KYCCaseId
      FROM
          UACTc75760ab10784b51b585f082d4b25223 AS MI,
          UACT175e55161660402692a53a4cdeb89bd6 AS MainTable,
          UACT5996d6e5151245cab24e4e76e3e53540 AS Statuses,
          UACTde5f05df6c5f4872a1e57b3cf8368301 AS AddressDetails
      WHERE
          (
          MI.CaseStartDateTime BETWEEN ' + quotename(convert(varchar(10), @CaseStartDateTime, 120), '''') + ' AND '  + quotename(convert(varchar(10), @CaseEndDateTime, 120), '''') +
          ' OR
          MI.CaseEndDateTime BETWEEN ' + quotename(convert(varchar(10), @CaseStartDateTime, 120), '''') +  ' AND ' + quotename(convert(varchar(10), @CaseEndDateTime, 120), '''') +
          ' )
      AND
          MI.KYCCase_Id = MainTable.KYCCaseId'
      IF @StatusName IS NOT NULL AND ltrim(rtrim(@StatusName)) != N''
          SET @SQL = @SQL + ' 
          AND
              Statuses.SourceStatus = MainTable.Level1Status
          AND
              Statuses.StatusName = ' + quotename(@StatusName, '''')
      IF @CountryDscr IS NOT NULL AND ltrim(rtrim(@CountryDscr)) != N''
      SET @SQL = @SQL + '
          AND
              AddressDetails.CountryDscr = ' + quotename(@CountryDscr, '''')      
      IF @CellDscr IS NOT NULL AND ltrim(rtrim(@CellDscr)) != N''
          SET @SQL = @SQL + ' 
          AND
              MainTable.Cell_NameDescription = ' + quotename(@CellDscr, '''') 
      IF @QCAnalystName IS NOT NULL AND ltrim(rtrim(@QCAnalystName)) != N''
          SET @SQL = @SQL + '
          AND
              MainTable.QCAnalystName = ' + quotename(@QCAnalystName, '''')   
      IF @AnalystName IS NOT NULL AND ltrim(rtrim(@AnalystName)) != N''
          SET @SQL = @SQL + '
          AND
              MainTable.AnalystName = ' + quotename(@AnalystName, '''')
      IF @RequestingEntity IS NOT NULL AND ltrim(rtrim(@RequestingEntity)) != N''
          SET @SQL = @SQL + '
          AND
              MainTable.Requesting_Entity = ' + quotename(@RequestingEntity, '''')
      IF @EntityType IS NOT NULL AND ltrim(rtrim(@EntityType)) != N''
          SET @SQL = @SQL + '
          AND
              MainTable.Legal_Entity_TypeDescription = ' + quotename(@EntityType, '''')
      IF @HighPriority IS NOT NULL AND ltrim(rtrim(@HighPriority)) != N''
          SET @SQL = @SQL + '
          AND
              MainTable.HighPriorityDescription = ' + quotename(@HighPriority, '''')
      IF @DDLevelRequired IS NOT NULL AND ltrim(rtrim(@DDLevelRequired)) != N''
          SET @SQL = @SQL + '
          AND
              MainTable.DD_Level_RequiredDescription = ' + quotename(@DDLevelRequired, '''')  
  
  EXEC sp_executesql @SQL
  </code></pre>
  <p>This is the entire code, The error I'm getting is</p>
  <blockquote>
  <p>Msg 156, Level 15, State 1, Line 32<br>
  Incorrect syntax near the keyword 'Case'.</p>
  </blockquote>