Using a variable for table name in 'From' clause in SQL Server 2008
.net, sql, sql-server, t-sql
Solution
This can't be done, dynamic SQL is not supported in functions.
See this SO question: Executing dynamic SQL in a SQLServer 2005 function
Problem
I have a UDF that queries data out of a table. The table, however, needs to be definable as a parameter. For example I can't have: Select * From [dbo].[TableA] I need something like: Select * From [dbo].[@TableName] The above line doesn't work, and also the UDF prohibits me from setting the query as a string and calling exec(). I can do this in a procedure, but I can't call the procedure from the UDF either. Does anyone know how I can accomplish this within the UDF without having some kind of massive switch statement?