Table of Contents
  尾 语
Home Database Mysql Tutorial SQLServer分布式查询(2)

SQLServer分布式查询(2)

Jun 07, 2016 pm 03:27 PM
sqlserver distributed troubleshooting Inquire question

3问题排查与更多查询方式 当我们在实际编程中进行访问远程数据时 因为不同操作环境会引发各种各样的异常,如下我会提出一种常见的异常方式解决办法和关于远程数据操作更多查询方式. 3.1无法建立远程连接 其实这个问题在做分布式查询时极其常见. 而引起这个问

  问题排查与更多查询方式

  当我们在实际编程中进行访问远程数据时 因为不同操作环境会引发各种各样的异常,如下我会提出一种常见的异常方式解决办法和关于远程数据操作更多查询方式.

  无法建立远程连接

  其实这个问题在做分布式查询时极其常见. 而引起这个问题的因素过多. 我们一时无法判断真正引发这个异常地方. 只能通过逐个排查方式来进行设置:

  例如我们在建立关联关系后 进行查询时会遇到:

SQLServer分布式查询(2)

  提示是: 在进行远程连接时超时, 引起这个问题原因可能是远程服务器积极拒绝访问!

  首先要在Sql Server Configuation Manager中保证你服务已经运行 且是开机自动运行.

  再次检查SQl2005外围配置DataBaseEngine允许远程连接:

SQLServer分布式查询(2)

  设置完成后.我们还需要设置Sql Server Analysis Services分析服务也支持远程数据查询:

SQLServer分布式查询(2)

  在远程服务器上如果启用了防火墙则可能对目前SQl Server方位实例进行拦截. 所以在服务器端启用防火墙情况下要为SQl DAtaBase创建例外.防止客户端请求被拦截.

  进程被其他用户占用

  当我们在远程分布式查询中有创建动作或是类似创建一个新的数据库. 有时会提示 “该数据库无法操作 已经别其他进程占用”异常. 导致我们无法访问数据库. 或是执行我们要做的创建操作.

  遇到这种情况我们可以利用SA权限查询到Master数据库对应数据库被占用的进程 并杀掉Kill Process.查询:

<p><span>1</span><span>:  </span><span>--</span><span> [sysprocesses 表中保存关于运行在 Microsoft® SQL Server™ 上的进程的信息。</span><span><br>   </span><span>2</span><span>:  </span><span>--</span><span> 这些进程可以是客户端进程或系统进程。sysprocesses 只存储在 master 数据库中]</span><span><br></span><span>4</span><span>:  </span><span>use</span><span> Master<br>   </span><span>5</span><span>:  </span><span>go</span><span><br>   </span><span>7</span><span>:  </span><span>SELECT</span><span>*</span><span>FROM</span><span> sysprocesses ,sysdatabases </span><span>WHERE</span><span> sysprocesses.dbid</span><span>=</span><span>sysdatabases.dbid </span><span>AND</span><span> sysdatabases.Name</span><span>=</span><span>'</span><span>CustomerDB</span><span>'</span><span><br>   </span><span>9</span><span>:  </span><span>select</span><span>*</span><span>from</span><span> sysprocesses<br>  </span><span>11</span><span>:  </span><span>select</span><span>*</span><span>from</span><span> sysdatabases<br>  </span><span>13</span><span>:  </span><span>--</span><span> 杀死占用进程</span><span><br></span><span>14</span><span>:  </span><span>kill</span><span>5</span></p>
Copy after login

  当我们对进程占用清除时有可能访问数据库被系统进程占用. 则这时用Sa无法杀死.这时提示:

SQLServer分布式查询(2)

  “Only use Process can be Kill ”在SQl2005 只有只有用户进程才能Kill掉.

  更多的查询操作

  往往我们在实际操作中需要对数据读写有更多要求. 例如从远程连接多个服务器进行数据读取或是把本地数据提交到服务器上. 为了提高效率和性能采用分布式事务来进行批量操作等等. 如下简单介绍在分布式查询中多中数据操作:

  把远程数据导入本地:

<p><span>1</span><span>:  </span><span>--</span><span> 导入数据操作</span><span><br>   </span><span>2</span><span>:  </span><span>select</span><span>top</span><span>(</span><span>3</span><span>) </span><span>*</span><span>into</span><span> TestDB.dbo.CopyDb </span><span>from</span><span>[</span><span>192.168.10.76</span><span>]</span><span>.wl.dbo.Users<br></span></p>
Copy after login

  导入时使用Into方式 自动在本地创建CopyDB表完全复制远程服务器上Users表的数据结构.但是要注意在进行后 的CopyDB将不包含原表的主键和索引约束. 虽然能快构建 但是主键和索引设置都会丢失.

  本地数据导入远程:

<p><span>--</span><span> 把本地表导入远程表 [openWset方式]</span><span><br></span><span>insert</span><span>openrowset</span><span>( </span><span>'</span><span>SQLOLEDB </span><span>'</span><span>, </span><span>'</span><span>sql服务器名 </span><span>'</span><span>; </span><span>'</span><span>用户名 </span><span>'</span><span>; </span><span>'</span><span>密码 </span><span>'</span><span>,数据库名.dbo.表名)  </span><span>select</span><span>*</span><span>from</span><span> 本地表 <br></span><span>--</span><span> 把本地表导入远程表 [open Query方式]</span><span><br></span><span>insert</span><span>openquery</span><span>(ITSV, </span><span>'</span><span>SELECT * FROM 数据库.dbo.表名 </span><span>'</span><span>)  </span></p>
Copy after login
更新本地表数据:<br>
Copy after login

<p><span>1</span><span>:  </span><span>--</span><span> 把本地表导入远程表 [opendataSource方式]</span><span><br>   </span><span>2</span><span>:    </span><span>insert</span><span>opendatasource</span><span>( </span><span>'</span><span>SQLOLEDB </span><span>'</span><span>, </span><span>'</span><span>Data Source=ip/ServerName;User ID=登陆名;Password=密码 </span><span>'</span><span>).数据库.dbo.表名  <br>   </span><span>3</span><span>:   <br>   </span><span>4</span><span>:  </span><span>--</span><span> 更新本地表 [openowset方式]</span><span><br></span><span>5</span><span>:   </span><span>update</span><span> b  </span><span>set</span><span> b.列A</span><span>=</span><span>a.列A  </span><span>from</span><span>openrowset</span><span>( </span><span>'</span><span>SQLOLEDB </span><span>'</span><span>, </span><span>'</span><span>sql服务器名 </span><span>'</span><span>; </span><span>'</span><span>用户名 </span><span>'</span><span>; </span><span>'</span><span>密码 </span><span>'</span><span>,数据库名.dbo.表名)<br>   </span><span>6</span><span>:   </span><span>as</span><span> a </span><span>inner</span><span>join</span><span> 本地表 b  </span><span>on</span><span> a.column1</span><span>=</span><span>b.column1  </span></p>
Copy after login

  当然还有更多方式来操作分布式查询操作.各位都可以尝试.

  尾 语

  如上是我最近在项目中处理关于分布式查询涉及到方方面面. 从系统架构到分部是查询具体操作细节.基本都是一些非常基础运用.当然也参考不少资料.以及动手来验证整个过程出现问题原因所在. 篇幅有限 写的有些仓促. 难免有纰漏地方 还望各位指正.

Statement of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to import mdf file into sqlserver How to import mdf file into sqlserver Apr 08, 2024 am 11:41 AM

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

How to solve the problem that the object named already exists in the sqlserver database How to solve the problem that the object named already exists in the sqlserver database Apr 05, 2024 pm 09:42 PM

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

What to do if the sqlserver service cannot be started What to do if the sqlserver service cannot be started Apr 05, 2024 pm 10:00 PM

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

How to check sqlserver port number How to check sqlserver port number Apr 05, 2024 pm 09:57 PM

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

How to recover accidentally deleted database in sqlserver How to recover accidentally deleted database in sqlserver Apr 05, 2024 pm 10:39 PM

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

Where is the sqlserver database? Where is the sqlserver database? Apr 05, 2024 pm 08:21 PM

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How to delete sqlserver if the installation fails? How to delete sqlserver if the installation fails? Apr 05, 2024 pm 11:27 PM

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

See all articles