在SQL Server 2008中使用的表名变量“发件人”条款 [英] Using a variable for table name in 'From' clause in SQL Server 2008

查看:210
本文介绍了在SQL Server 2008中使用的表名变量“发件人”条款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

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

这篇关于在SQL Server 2008中使用的表名变量“发件人”条款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆