简体   繁体   中英

T-sql: Table valued function

We have a table with a column MyFunction nvarchar() that contains the name of a certain table valued function.

There are different functions so the number of columns can differ per function.

  1. Is there a way to call the function as a string?

    For example FunctionA is a function, Set @MyFunction = 'FunctionA' then call MyFunction('x','y')

  2. Is there a way to store the results in a tempTable since we don't know the number of columns up front?

Thanks in advance

  1. Yes, you can do it using dynamic SQL:

     EXEC sp_executesql N'select * from MyFunction(''x'', ''y'')' 
  2. Yes, you can, but you need global temp table:

     EXEC sp_executesql N'select * into ##test from MyFunction(''x'', ''y'')'; select * from ##test; 

Hope this works for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM