# Linked Server

## MÔ HÌNH

Hiện tại có 02 phiên bản phần mềm QT và CB cài đặt ở 02 server khác nhau, cần truyền dữ liệu từ bản QT sang bản CB.

Đứng ở Server của bản CB ( đích) tạo 1 Linked Server để kết nối tới server bản QT

## CÁCH THỰC HIỆN

### Dùng lệnh T-sql

1. Tạo Linked Server dùng lệnh sau thay đổi thông tin phù hợp

```sql
DECLARE @LinkedServerName NVARCHAR(100) = N'LINKED_DTECH';  -- Tên của Linked server
DECLARE @RemoteServerIP   NVARCHAR(100) = N'IP\Dtech_Express,2866'; -- Thay IP thực tế
DECLARE @RemoteUser       NVARCHAR(100) = N'sa';          
DECLARE @RemotePassword   NVARCHAR(100) = N'xxxx'; -- Thay đổi mật khẩu user sa

-- 1. Xóa nếu tồn tại
IF EXISTS (SELECT srvname FROM master.dbo.sysservers WHERE srvname = @LinkedServerName)
BEGIN
    EXEC master.dbo.sp_dropserver @server=@LinkedServerName, @droplogins='droplogins';
END

-- 2. Tạo Linked Server với SQLNCLI (Driver có sẵn phổ biến)
EXEC master.dbo.sp_addlinkedserver 
    @server = @LinkedServerName, 
    @srvproduct = N'',           
    @provider = N'SQLNCLI',    -- Đã đổi từ MSOLEDBSQL sang SQLNCLI
    @datasrc = @RemoteServerIP;

-- 3. Cấu hình Login
EXEC master.dbo.sp_addlinkedsrvlogin 
    @rmtsrvname = @LinkedServerName, 
    @useself = N'False', 
    @rmtuser = @RemoteUser, 
    @rmtpassword = @RemotePassword;

-- 4. Bật cấu hình RPC
EXEC master.dbo.sp_serveroption @server=@LinkedServerName, @optname=N'rpc', @optvalue=N'true';
EXEC master.dbo.sp_serveroption @server=@LinkedServerName, @optname=N'rpc out', @optvalue=N'true';

PRINT '--- Da tao Linked Server ' + @LinkedServerName + ' bang SQLNCLI thanh cong! ---';

```

2. Kiểm tra Linked server có kết nối thành công

```sql
DECLARE @LinkedServerName NVARCHAR(100) = N'LINKED_DTECH'; 
PRINT '--- Dang kiem tra ket noi den ' + @LinkedServerName + '... ---';
EXEC('SELECT 1') AT [LINKED_DTECH]; -- Thay ten neu ban doi @LinkedServerName
PRINT '--- Ket noi thanh cong! ---';
```

3. Thực hiện cập nhật giao diện truyền đẩy dữ liệu lên có 02 lưới

Data nguồn trên giao diện cấu hình sửa lại theo cú pháp:

```
LINKED_DTECH.CONGTY_ABC_QT
```

### Dùng giao diện Manager Studio


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kythuat.dtechvn.com/sql-server/linked-server.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
