SHARE
TWEET

Assign function result to a table variable

a guest Feb 26th, 2012 20 Never
  1. Declare @Data as table (FieldValue varchar(100))
  2. insert into @Data select * from MyFunction ('Person.Address','AddressID')    
  3.  
  4. -- Function
  5. Alter function MyFunction (
  6.    @TableName varchar(100), @FieldName varchar(100)
  7. ) returns @GetData table (
  8.    FieldValue  varchar(100)
  9. ) as
  10. begin
  11.         Declare @SQL varchar(250)
  12.         Set @SQL = 'Select '+@FieldName+ ' from '+ @TableName
  13.         Exec sp_executesql @SQL    
  14.         return
  15. end
  16.        
  17. INSERT @TableVariable
  18. EXEC spYourProcedure
  19.        
  20. INSERT @TableVariable (FieldInSp1, FieldInSp2)
  21. EXEC spYourProcedure
RAW Paste Data
Top