title: Tricks on how to execute string inside a function in Sql Server
body: <p>Procedure FunctionX, Line 345</p>
  
  <blockquote>
    <p>Invalid use of a side-effecting operator 'EXECUTE STRING' within a
    function.</p>
  </blockquote>
  
  <p>I get the above error when I execute a dynamic statement inside a function in SQL Server 2012.
  Is there a workaround for this? Any tricks? </p>
  
  <p>PS: The sproc (stored procedure) is much too lengthy for its body to be inserted as-is inside the function.</p>
  
  <pre><code>DECLARE @execsql NVARCHAR(2000)
  Set @execsql = 'INSERT INTO @TABLE1 EXEC SPROC1 ' + @ID_COMPANY + ',' + @ID_COUNTRY 
  exec (@execsql)
  </code></pre>
  
  <p>Many thanks in advance.</p>
  
  <p>Also, I need to be able to delete inside the function as well. I know this contradicts the definition of functions but I am wondering if there are some tricks that can be used</p>