title: Send query as parameter to SQL function
body: <p>I want to create a SQL tabled-value function that will receive a query as n parameter through my API. In my function I want execute that query. The query will be a SELECT statement.</p>
  
  <p>This is what I have done so far and what to achieve but it is not the correct way to do so.</p>
  
  <pre><code>CREATE FUNCTION CUSTOM_EXPORT_RESULTS ( 
      @query varchar(max),
      @guid uniqueidentifier,
      @tableName varchar(200))
  RETURNS TABLE 
  AS
  RETURN 
  (
      -- Execute query into a table
      SELECT * 
      INTO @tableName
      FROM (
          EXEC(@query)
      ) 
  )
  GO
  </code></pre>
  
  <p>Please suggest the correct way!</p>