在T-SQL中评估 [英] Evaluate in T-SQL

查看:103
本文介绍了在T-SQL中评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,该存储过程允许使用IN参数指定要使用的数据库.然后,我在该数据库中使用预先确定的表进行查询.我遇到的问题是在查询中将表名连接到该数据库名.如果T-SQL具有评估功能,我可以做类似的事情

I've got a stored procedure that allows an IN parameter specify what database to use. I then use a pre-decided table in that database for a query. The problem I'm having is concatenating the table name to that database name within my queries. If T-SQL had an evaluate function I could do something like

eval(@dbname + 'MyTable')

当前,我坚持创建一个字符串,然后使用exec()来运行该字符串作为查询.这是一团糟,我宁愿不必创建一个字符串.有没有一种方法可以评估变量或字符串,以便可以执行以下操作?

Currently I'm stuck creating a string and then using exec() to run that string as a query. This is messy and I would rather not have to create a string. Is there a way I can evaluate a variable or string so I can do something like the following?

SELECT *
FROM eval(@dbname + 'MyTable')

我希望它进行评估,以便最终看起来像这样:

I would like it to evaluate so it ends up appearing like this:

SELECT *
FROM myserver.mydatabase.dbo.MyTable

推荐答案

阅读此... 诅咒和动态SQL的祝福,使我对如何解决此类问题有很多了解.

Read this... The Curse and Blessings of Dynamic SQL, help me a lot understanding how to solve this type of problems.

这篇关于在T-SQL中评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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