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

1. KEY MẸ CON BỊ TRÙNG

// 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

/** 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
/**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

---- 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

Last updated