-- 범용 데이터 품질 검증 스크립트
DECLARE target_table STRING DEFAULT 'catalog.silver.orders';
DECLARE target_date DATE DEFAULT CURRENT_DATE() - INTERVAL 1 DAY;
DECLARE null_count INT;
DECLARE dup_count INT;
DECLARE negative_count INT;
DECLARE total_issues INT DEFAULT 0;
-- 1. NULL 체크
SET null_count = (
SELECT COUNT(*)
FROM IDENTIFIER(target_table)
WHERE order_id IS NULL AND order_date = target_date
);
SET total_issues = total_issues + null_count;
-- 2. 중복 체크
SET dup_count = (
SELECT COUNT(*) - COUNT(DISTINCT order_id)
FROM IDENTIFIER(target_table)
WHERE order_date = target_date
);
SET total_issues = total_issues + dup_count;
-- 3. 음수 금액 체크
SET negative_count = (
SELECT COUNT(*)
FROM IDENTIFIER(target_table)
WHERE amount < 0 AND order_date = target_date
);
SET total_issues = total_issues + negative_count;
-- 4. 결과 기록
INSERT INTO audit.quality_checks (table_name, check_date, null_count, dup_count, negative_count, total_issues, checked_at)
VALUES (target_table, target_date, null_count, dup_count, negative_count, total_issues, CURRENT_TIMESTAMP());
-- 5. 임계치 초과 시 오류 발생
IF total_issues > 0 THEN
SIGNAL SQLSTATE '45001'
SET MESSAGE_TEXT = CONCAT(
'데이터 품질 이슈 발견: NULL=', null_count,
', 중복=', dup_count,
', 음수금액=', negative_count
);
END IF;
SELECT 'Quality check passed' AS result;