使用sql server查询远程数据库? [英] quering remote database using sql server?

查看:69
本文介绍了使用sql server查询远程数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 sp_addlinkedserver 来访问远程机器数据库,现在我正在像这样的数据库明确地编写查询,

I have used sp_addlinkedserver to access the remote machines db now i am writing queries explicitly on database like,

select * from [server\instance].database.owner.tablename

select * from [server\instance].database.owner.tablename

现在有了这个,

  1. [Server\instance] : 这必须明确提供
  2. [database] :我们可以使用 ms_ForEachDB 之类的查询在指定实例上找到数据库吗?
  3. [owner] : 我们可以使用查询找到数据库所有者名称吗?

如果这些值是使用查询找到的,我们是否需要使用 EXEC() 来执行它,或者我们仍然可以使用漂亮的查询来实现它?

If these values are found using queries do we need to use EXEC() to execute this or we can still achieve it using nice queries ?

谢谢大家

推荐答案

您提到的不错"格式只是一个由 4 部分组成的对象引用.

The "nice" format you mention is simply a 4 part object reference.

select * from [server\instance].database.owner.tablename

3 部分

select * from database.owner.tablename

2 部分

select * from owner.tablename

如果您想动态更改任何服务器、数据库或架构值,那么您有一个选择:

If you want to dynamically change any of the server, db or schema values then you have one option:

EXEC (@sqlstring)

但是,如果您只远程访问存储过程...

However, if you only access stored procs remotely...

DECLARE @RemoteSP varchar(500)

SET @RemoteSP = '[server\instance].database2.schema.proc2'
EXEC @RemoteSP @p1, @p2, @p3 OUTPUT

SET @RemoteSP = '[server\instance].database1.schema.proc1'
EXEC @RemoteSP @p4, @p5, @p6 OUTPUT

但是,更改对象引用的组件毫无意义:如果您知道要查询一个表,那么只需在该数据库中调用该表...

However, changing the components of the object reference makes no sense arguably: if you know you're going to query a table then just call that table in that database...

这篇关于使用sql server查询远程数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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