> For the complete documentation index, see [llms.txt](https://kythuat.dtechvn.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kythuat.dtechvn.com/cai-dat-dtech/x-ly-lien-quan-key-he-thong.md).

# XỬ LÝ LIÊN QUAN KEY HỆ THỐNG

## 1. KEY MẸ CON BỊ TRÙNG

```sql
// KEYMECON bị trùng, dẫn đến báo cáo vật tư chi tiết lấy đơn giá bán bị sai
// xử lý, gán vattu vào keymecon. có thể sai nếu vattu theo lenhsanxuat
// thì vattu xuất hiện 2 dòng
Update  DuLieuKeToan set KeyMeCon = VatTu
from
(
select a,Max(KeyCT) as KeyCt,count(*) as dongtrung
from
(
select ltrim(rtrim(str(keyct)))+'_'+ ltrim(rtrim(str(KeyMeCon))) as a,KeyCT from DulieuKeToan where psco=0 group by KeyCT,KeyMeCon,VatTu
)
temp
group by a having count(*)>1
)a 
where DulieuKeToan.KeyCt = a.KeyCT and DulieuKeToan.VatTu>0
```

## 2.UPDATE LẠI KEYCT

```sql
/** Update lại hết Keyct về =0 hết rồi mới update lại.**/
Update DuLieuKeToan
set KeyCT=0 
Where NghiepVu=301 -- Tuỳ điều kiện where cần đánh lại
```

```sql
/**Update lại Keyct nếu Keyct=0**/
update DuLieuKeToan set KeyDong = Case When KeyDong=0 then identityKey else KeyDong end
where KeyDong =0
update DuLieuKeToan set KeyCT = case when KeyCT=0 then a.KeyCTnew else KeyCT end
from
(
select ChungTu,Ngay,NghiepVu,MIN(identityKey) as KeyCTnew from DuLieuKeToan where KeyCT = 0 group by ChungTu,Ngay,NghiepVu
)a
where (KeyCT =0 and DuLieuKeToan.ChungTu = a.ChungTu and DuLieuKeToan.Ngay = a.Ngay and DuLieuKeToan.NghiepVu = a.NghiepVu)
```

## 3. KIỂM TRA KEYCT

```sql
---- KIỂM TRA TRÙNG KEYCT
select keyct from
(
select keyct,ChungTu from DuLieuKeToan
group by KeyCT,ChungTu
)a  group by KeyCT
having COUNT (*)>1

---- UPDATE KEYCT TRÙNG (CHUNGTU SỐ 2 TRỜ ĐI) VỀ = 0
update DuLieuKeToan set KeyCT=0 where KeyCT in (

select keyct from
(
select keyct,ngay,NghiepVu from DuLieuKeToan
group by keyct,ngay,NghiepVu,chungtu
)a group by KeyCT
having COUNT(*)>1)

----- ĐÁNH LẠI KEYCT
update DuLieuKeToan set KeyDong = Case When KeyDong=0 then identityKey else KeyDong end
where KeyDong =0
update DuLieuKeToan set KeyCT = case when KeyCT=0 then a.KeyCTnew else KeyCT end
from
(
select ChungTu,Ngay,NghiepVu,MIN(identityKey) as KeyCTnew from DuLieuKeToan where KeyCT = 0 group by ChungTu,Ngay,NghiepVu
)a
where (KeyCT =0 and DuLieuKeToan.ChungTu = a.ChungTu and DuLieuKeToan.Ngay = a.Ngay and DuLieuKeToan.NghiepVu = a.NghiepVu)
```

## 4. XỬ LÝ SỐ CHỨNG TỪ PHIẾU XUẤT BÁN HÀNG DO NĂM 23 CHƯA CÓ

```
-- 301 NGHIỆP VỤ BÁN HÀNG - 201 PHIẾU XUẤT - PSCO=11 BT THÊM XUẤT KHO
update dulieuketoan 
set nghiepvukemtheo=201
where nghiepvu=301 and psco=11 and nghiepvukemtheo=0
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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/cai-dat-dtech/x-ly-lien-quan-key-he-thong.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.
