在SQL Server 2008中使用的表名变量“发件人”条款发件人、变量、条款、SQL

2023-09-02 10:33:36 作者:烈酒与老友

我有一个UDF,查询出的数据表中。表,然而,需要定义作为参数。例如,我不能有:

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]。[表A]

我需要的是这样的:

SELECT * FROM [DBO]。[@表名]

以上行不行,也是UDF禁止我从设置的查询作为一个字符串,并调用exec()。我可以在过程中做到这一点,但我不能把从UDF的程序无论是。

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.

有谁知道我怎么能在UDF内做到这一点,而无需某种巨大的switch语句?

Does anyone know how I can accomplish this within the UDF without having some kind of massive switch statement?

推荐答案

这不能做,动态SQL是不支持的功能。

This can't be done, dynamic SQL is not supported in functions.

请参阅这太问题: http://stackoverflow.com/questions/150552/executing-dynamic-sql-in-a-sqlserver-2005-function