0

I need to create a function in sql server that receives a table name. The table name will be used to create a select statement and return a result set. The result set will be processed and returned back.

Something like this:

SELECT * FROM @table_name WHERE id=1
chresse
  • 5,486
  • 3
  • 30
  • 47
  • 1
    In order to pass a table name to a *function* and run SQL on the table, you need dynamic SQL. SQL Server user-defined functions don't support dynamic SQL. Hence, this would not seem to be possible: http://msdn.microsoft.com/en-us/library/ms191320.aspx. – Gordon Linoff Aug 07 '14 at 12:10
  • @JonEgerton . . . I strongly disagree that that question is a duplicate. This question is about *functions* not *stored procedures*. The restrictions are quite different in SQL Server. – Gordon Linoff Aug 07 '14 at 12:18
  • Gordon is correct. Dynamic SQL is not supported in a UDF. So how about alternatives. How about receiving a table as a parameter http://stackoverflow.com/questions/1609115/pass-table-as-parameter-into-sql-server-udf – tgolisch Aug 07 '14 at 14:11

0 Answers0