T-SQL 表名别名 [英] T-SQL Table name alias

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

问题描述

在我的 T-SQL 脚本中,我多次引用相同的长表名.我在不同的表上使用此查询.

In my T-SQL script, I refer to same long table name several times. I use this query on different tables.

有没有办法通过变量引用表名?如果是这样,我可以简单地在脚本将使用的顶部声明一个变量,只需设置值,我就可以在各种表上运行它,而无需更改脚本.

Is there a way to refer a table name by variable? If so, I can simple declare one variable at the top which script will use and just by setting value, I can run it on various tables without making changes in the script.

推荐答案

您可以为该表创建同义词,但显然您需要确保在脚本运行时没有人更改同义词的定义(并且没有脚本的并行调用)

You could create a synonym for that table but obviously you'd need to make sure that nobody changed the definition of the synonym whilst the script was running (and no parallel invocations of the script)

您是否在 SSMS 中运行这些?如果是这样,您可以设置 SQL CMD 模式(在查询"菜单上)并使用

Are you running these in SSMS? If so you could set SQL CMD mode (on the "Query" menu) and use

:setvar tablename "spt_values" 

use master

select * from $(tablename)

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

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