Several time ago I used the "
ISBN mod 11 Check" to write a little, crappy trigger for validating ISBN numbers. It excepts a nine numbers long ISBN number as NUMBER() and the last digit of the ISBN as CHAR().
It would be possible to use "standard" ISBN formats like "0-7357-1201-8" (surreptitious advertising :oP) for the trigger too. The ISBN must/should be specified as VARCHAR2() - we can easily replace the hyphen ('-') with the replace() function (see comments in the source).
CREATE OR REPLACE TRIGGER check_isbn
BEFORE INSERT ON buch
FOR EACH ROW
DECLARE
-- isbn is nine chars long and exists of numbers only
new_isbn NUMBER;
new_check_digit CHAR;
char_at NUMBER;
summe NUMBER;
i NUMBER;
BEGIN
new_isbn := TO_CHAR(:new.isbn);
new_check_digit := TO_CHAR(:new.ISBN_Pruefziffer);
summe := 0;
-- Replace '-' to ''
-- replace(new_isbn, '-');
-- We should have 9 numbers in new_isbn
IF NOT LENGTH(new_isbn) = 9 THEN
RAISE_APPLICATION_ERROR(-20012, 'The ISBN number supplied is invalid!');
END IF;
-- Loop 9 times from 10 to 2
FOR i IN REVERSE 2..10
LOOP
-- Get position of current number
char_at := (11 - i);
summe := summe + (i * TO_NUMBER(SUBSTR(new_isbn, char_at, 1)));
END LOOP;
-- Check for 'X' as last digit
-- Add the value of 'new_check_digit' to 'sum'
IF new_check_digit = 'X' THEN
summe := summe + 10;
ELSE
summe := summe + TO_NUMBER(new_check_digit);
END IF;
IF NOT mod(summe, 11) = 0 THEN
RAISE_APPLICATION_ERROR(-20012, 'The ISBN number supplied is invalid!');
END IF;
END;
/