search
HomeDatabaseMysql TutorialSQLServer跨库查询

用openrowset连接远程SQL或插入数据 --如果只是临时访问,可以直接用openrowset -- 查询 示例 select*fromopenrowset('SQLOLEDB' ,'sql服务器名';'用户名';'密码' ,数据库名.dbo.表名) --导入示例 select*into表fromopenrowset('SQLOLEDB' ,'sql服务器名';'用

用openrowset连接远程SQL或插入数据

--如果只是临时访问,可以直接用openrowset

--查询示例

SQLServer跨库查询select * from openrowset('SQLOLEDB'

SQLServer跨库查询,'sql服务器名';'用户名';'密码'

SQLServer跨库查询,数据库名.dbo.表名)

SQLServer跨库查询

SQLServer跨库查询

SQLServer跨库查询--导入示例

SQLServer跨库查询select * into 表 from openrowset('SQLOLEDB'

SQLServer跨库查询,'sql服务器名';'用户名';'密码'

SQLServer跨库查询,数据库名.dbo.表名)

SQLServer跨库查询

SQLServer跨库查询--创建链接服务器

SQLServer跨库查询exec sp_addlinkedserver  'srv_lnk','','SQLOLEDB','远程服务器名或ip地址'

SQLServer跨库查询exec sp_addlinkedsrvlogin 'srv_lnk','false',null,'用户名','密码'

SQLServer跨库查询go

SQLServer跨库查询

SQLServer跨库查询--查询示例

SQLServer跨库查询select * from srv_lnk.数据库名.dbo.表名

SQLServer跨库查询

SQLServer跨库查询--导入示例

SQLServer跨库查询select * into 表 from srv_lnk.数据库名.dbo.表名

SQLServer跨库查询

SQLServer跨库查询--以后不再使用时删除链接服务器

SQLServer跨库查询exec sp_dropserver 'srv_lnk','droplogins'

SQLServer跨库查询go

--下面的示例访问来自某个表的数据,该表在 SQL Server 的另一个实例中。

SELECT *FROM OPENDATASOURCE('SQLOLEDB','Data Source=ServerName;User ID=MyUID;Password=MyPass').Northwind.dbo.Categories

下面是个查询的示例,它通过用于 Jet 的 OLE DB 提供程序查询 Excel 电子表格。

SELECT *FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source="c:Financeaccount.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions

 

例如:

SELECT *

FROM OPENQUERY(Far_MDB, 'SELECT * FROM MDB_Member')

这就是从我的链接服务器当中去查询MDB_Member表的数据

备注:

1、关于链接服务器的详细情况请参阅联机丛书中的sp_addlinkedserver、 sp_addlinkedsrvlogin、sp_addserver、sp_dropserver、sp_serveroption、sp_linkedservers等系统级存储过程。

2、关于此链接服务器的创建及其使用在不同环境下(全部内网服务器、一内网一公网服务器、全公网服务器)测试,均告通过。

3、有时候在存储过程中访问链接服务器的时候可能会碰到如下的提示,必须要对ANSI warning 和ANSI nulls进行设置,这时候按照如下的方法进行设置:

设置本地数据库的属性-->连接-->选上ANSI warning 和 ANSI nulls

如何用sql语句进行跨库查询

在机器B上的数据库database2中创建一个临时表#tmp,

内容就是机器A上的数据库database1里面的Table1。

如何用sql语句完成?(不借用dts等)

SQL code --创建链接服务器

exec sp_addlinkedserver  'ITSV','','SQLOLEDB','远程服务器名或ip地址'

exec sp_addlinkedsrvlogin 'ITSV','false',null,'用户名','密码'

--查询示例

select * from ITSV.数据库名.dbo.表名

--导入示例

select * into 表 from ITSV.数据库名.dbo.表名

--以后不再使用时删除链接服务器

exec sp_dropserver 'ITSV','droplogins'

--连接远程/局域网数据(openrowset/openquery/opendatasource)

--1、openrowset

--查询示例

select * from openrowset('SQLOLEDB','sql服务器名';'用户名';'密码',数据库名.dbo.表名)

--生成本地表

select * into 表 from openrowset('SQLOLEDB','sql服务器名';'用户名';'密码',数据库名.dbo.表名)

--把本地表导入远程表

insert openrowset('SQLOLEDB','sql服务器名';'用户名';'密码',数据库名.dbo.表名)

select *from 本地表

--更新本地表

update b

set b.列A=a.列A

from openrowset('SQLOLEDB','sql服务器名';'用户名';'密码',数据库名.dbo.表名)as a inner join 本地表 b

on a.column1=b.column1

--openquery用法需要创建一个连接

--首先创建一个连接创建链接服务器

exec sp_addlinkedserver  'ITSV','','SQLOLEDB','远程服务器名或ip地址'

--查询

select *

FROM openquery(ITSV, 'SELECT *  FROM 数据库.dbo.表名')

--把本地表导入远程表

insert openquery(ITSV, 'SELECT *  FROM 数据库.dbo.表名')

select * from 本地表

--更新本地表

update b

set b.列B=a.列B

FROM openquery(ITSV, 'SELECT * FROM 数据库.dbo.表名') as a

inner join 本地表 b on a.列A=b.列A

--3、opendatasource/openrowset

SELECT   *

FROM   opendatasource('SQLOLEDB', 'Data Source=ip/ServerName;User ID=登陆名password=密码' ).test.dbo.roy_ta

--把本地表导入远程表

insert opendatasource('SQLOLEDB', 'Data Source=ip/ServerName;User ID=登陆名password=密码').数据库.dbo.表名

select * from 本地表

用   链接服务器

OPENROWSET

OPENDATASOURCE

都可以

链接服务器.database1.dbo.table1

链接服务器.database1.dbo.table1

SQL code

select * into #

from openrowset('sqloledb','ip';'sa';'','select * from pubs.dbo.jobs')

如果只是偶尔使用   就用opendatasource/openrowset   固定的频繁使用建linked   server

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor