title: Invalid use of a side-effecting operator 'EXECUTE STRING' within a function error message
body: <p>I have searched for this error online and tried to use dynamic SQL. I am still getting 'Invalid use of a side-effecting operator 'EXECUTE STRING' within a function' Please let me know if there are any suggestions. I appreciate any help. </p>
  
  <pre><code>  ALTER FUNCTION [dbo].[fn_get_type_ids]
      (
          @category_id  int = null, 
          @all_id          int = null,
          @category_id_op varchar(20) = null,
          @all_op  varchar(20) = null,
      )  
      RETURNS @TypeIDs table
      (
          type_id int
      )
      AS
      BEGIN
  
  
          IF (@category_id_op = 'Is')
          BEGIN
              SET @category1 = ('level_1_id = @category_id')
          END
          ELSE IF (@category_id_op = 'IS NOT')
          BEGIN
              SET @category1 = ('level_1_id &lt;&gt; @category_id OR level_1_id IS NULL') 
          END    
  
          IF (@all_op = 'Is')
          BEGIN
              SET @category2 = ('level_2_id = @all_id')
          END
          ELSE IF (@all_op = 'Is NOT')
          BEGIN
              SET @category2 = ('level_2_id &lt;&gt; @all_id OR level_1_id IS NULL') 
          END    
  
  
              DECLARE @sql nvarchar(4000)
  
             set @sql = 'insert into @TypeIDs 
                       SELECT atr.type_id
                FROM type_rel atr 
                        LEFT JOIN category_ref acr1 ON atr.level_1_id = acr1.category_id
                        WHERE (' + @category1 + ' AND ' +  @category2 + ')' 
  
  
      exec (@sql)
  
       RETURN 
      END
  </code></pre>