如何通过链接服务器使用表名的同义词来截断表? [英] How do I truncate a table via linked server using a synonym for the table name?

查看:102
本文介绍了如何通过链接服务器使用表名的同义词来截断表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以执行以下操作:

I know I can do the following:

EXEC Server_Name.DBName.sys.sp_executesql N'TRUNCATE TABLE dbo.table_name'

但是如果我想为表使用同义词怎么办?

But what if I want to use a synonym for the table?

我在SERVER1上,我想使用表名的同义词来截断SERVER2上的表.

I'm on SERVER1 and I want to truncate a table on SERVER2 using a synonym for the table name.

这可能吗?

推荐答案

正确答案上的链接已断开.我遇到了类似的问题.我的解决方法是使用同义词表来查找基础表名,然后运行动态sql语句.据记录,同义词不能与TRUNCATE一起使用,但这至少是一个不错的解决方法.

The link on the correct answer is broken. I ran into a similar problem. My workaround was using the synonyms table to lookup underlying table name, then running a dynamic sql statement. It is documented that synonyms cannot be used with TRUNCATE, but at least this is a decent workaround.

DECLARE @TableName VARCHAR(500) = (SELECT TOP 1 base_object_name
    FROM Server_Name.DBName.sys.synonyms WHERE name = 'table_name')
DECLARE @Sql NVARCHAR(MAX) = 'EXEC Server_Name.DBName.sys.sp_executesql N''TRUNCATE TABLE ' + @TableName + ''''
EXEC sys.sp_executesql @Sql

这篇关于如何通过链接服务器使用表名的同义词来截断表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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