使用sql server查询远程数据库?

2022-10-21数据库问题
1

本文介绍了使用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查询远程数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End
SQLServer SQL Server

相关推荐

如何在 sql server 2012 中部署现有的 SSIS 包?
How to deploy a existing SSIS Package in sql server 2012?(如何在 sql server 2012 中部署现有的 SSIS 包?)...
2024-04-16 数据库问题
11

“无法连接到本地 MySQL 服务器"在 docker-compose 中
quot;Can#39;t connect to local MySQL serverquot; in docker-compose(“无法连接到本地 MySQL 服务器在 docker-compose 中)...
2024-04-16 数据库问题
51

在 SQL Server 中设计 1:1 和 1:m 关系
Designing 1:1 and 1:m relationships in SQL Server(在 SQL Server 中设计 1:1 和 1:m 关系)...
2024-04-16 数据库问题
2

主键的 Sql 数据类型 - SQL Server?
Sql Data Type for Primary Key - SQL Server?(主键的 Sql 数据类型 - SQL Server?)...
2024-04-16 数据库问题
4

SQL Server:如何限制表包含单行?
SQL Server: how to constrain a table to contain a single row?(SQL Server:如何限制表包含单行?)...
2024-04-16 数据库问题
3

SQL Server 中数据库范围的唯一但简单的标识符
Database-wide unique-yet-simple identifiers in SQL Server(SQL Server 中数据库范围的唯一但简单的标识符)...
2024-04-16 数据库问题
4