IF (CASE WHEN %new.line_abbrev IS NULL THEN 0 ELSE 1 END + CASE WHEN %new.product_category IS NULL THEN 0 ELSE 1 END) <> 1 THEN RAISERROR 18251 '%TriggerName: Must have a value for only one of LINE_ABBREV and PRODUCT_CATEGORY (the other must be NULL).'; END IF; IF %new.line_abbrev IS NULL AND %new.group_code IS NOT NULL THEN RAISERROR 18252 '%TriggerName: Cannot have value for GROUP_CODE without a value for LINE_ABBREV.'; END IF; IF (CASE WHEN %new.price_level IS NULL THEN 0 ELSE 1 END + CASE WHEN %new.ref_price_sheet_cd IS NULL THEN 0 ELSE 1 END) <> 1 THEN RAISERROR 18253 '%TriggerName: Must have a value for exactly one of PRICE_LEVEL and REF_PRICE_SHEET_CD (the other must be NULL).'; END IF; IF (CASE WHEN %new.ref_price_sheet_cd IS NULL THEN 0 ELSE 1 END + CASE WHEN %new.markup_percent IS NULL THEN 0 ELSE 1 END) = 1 THEN RAISERROR 18254 '%TriggerName: Either REF_PRICE_SHEET_CD and MARKUP_PERCENT both must have a value or neither must have a value.'; END IF; IF %new.effective_from_date > %new.effective_to_date THEN RAISERROR 18255 '%TriggerName: EFFECTIVE_TO_DATE cannot be earlier than EFFECTIVE_FROM_DATE.'; END IF; IF %new.line_abbrev IS NOT NULL THEN -- Prevent updates if there are any conflicting records. The user must clean up any conflicts before updating. IF EXISTS (SELECT * FROM %TableName WHERE customer_segment_id = %new.customer_segment_id AND product_category IS NULL AND line_abbrev = %new.line_abbrev AND ISNULL(group_code, -1) = ISNULL(%new.group_code, -1) AND ( effective_from_date BETWEEN %new.effective_from_date AND %new.effective_to_date OR effective_to_date BETWEEN %new.effective_from_date AND %new.effective_to_date OR %new.effective_from_date BETWEEN effective_from_date AND effective_to_date ) AND id <> %new.id ) THEN RAISERROR 18260 '%TriggerName: Record with conflicting criteria already exists.'; END IF; ELSE -- Prevent updates if there are any conflicting records. The user must clean up any conflicts before updating. IF EXISTS (SELECT * FROM %TableName WHERE customer_segment_id = %new.customer_segment_id AND product_category = %new.product_category AND line_abbrev IS NULL AND group_code IS NULL AND ( effective_from_date BETWEEN %new.effective_from_date AND %new.effective_to_date OR effective_to_date BETWEEN %new.effective_from_date AND %new.effective_to_date OR %new.effective_from_date BETWEEN effective_from_date AND effective_to_date ) AND id <> %new.id ) THEN RAISERROR 18260 '%TriggerName: Record with conflicting criteria already exists.'; END IF; END IF;