title: Assign function result to a table variable
body: <p>The SQL Server (2000/2005) function gets the table name and the field name as parameters and returns results from a dynamic query within the function. The results should be assigned to a <code>Table</code> variable which will be used further in a stored procedure.  How to achieve this?</p>
  
  <p>I am getting error: "Only functions and extended stored procedures can be executed from within a function."</p>
  
  <pre><code>Declare @Data as table (FieldValue varchar(100))
  insert into @Data select * from MyFunction ('Person.Address','AddressID')     
  
  -- Function
  Alter function MyFunction (
     @TableName varchar(100), @FieldName varchar(100) 
  ) returns @GetData table (
     FieldValue  varchar(100) 
  ) as
  begin
          Declare @SQL varchar(250)
          Set @SQL = 'Select '+@FieldName+ ' from '+ @TableName
          Exec sp_executesql @SQL     
          return
  end
  </code></pre>