This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Assign function result to a table variable

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 0.55 KB  |  views: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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
clone this paste RAW Paste Data