I don't know if this is possible and I couldn't understand what Books Online
says about this (there's a surprise!), but...
Is it possible to declare a variable (not an input or output parameter)
inside a table-valued function? I need to make the select statement dynamic,
depending on the input parameter, so I need to declare something like:
DECLARE @sql VARCHAR(250)
The resulting sql would be generated via IF/CASE conditions. As the whole
sql statement is quite large, the majority would be placed in the @sql
variable at the beginning. The "where" clause is the bit that is dynamic,
which would be appended to the @sql string.
Is it possible to declare variables in such a function, apart from the
input/output parameters?
Thanks,
SQL Servant
Aaron Bertrand [SQL Server MVP] - 31 Jul 2008 17:39 GMT
You can declare variables inside a function (this just changes it from an
inline to a multi-step function, and has performance implications). But you
CANNOT execute dynamic SQL in the function. Use a procedure.
On 7/31/08 12:36 PM, in article
920F876C-1ED6-4386-9589-FE7D5E7A9040@microsoft.com, "SQL Servant"
<SQLServant@discussions.microsoft.com> wrote:
> I don't know if this is possible and I couldn't understand what Books Online
> says about this (there's a surprise!), but...
[quoted text clipped - 15 lines]
> Thanks,
> SQL Servant