plsql - PL/SQL: trigger error on altering insert data -
i'm trying write trigger table of books. table,
create table books ( isbn varchar(13) check (length (isbn) = 10 or length (isbn) = 13), ... primary key (isbn) );
i want write trigger when isbn of length 10 inserted, change format 13-digit 1 according following rules:
- last digit removed
- added 978 front
added check bit calculated formula
check_bit = (10 - (x1 + 3*x2 + x3 + 3*x4 + ... + x11 + 3*x12) mod 10) mod 10
code:
create or replace trigger isbnformatchange before insert on books each row begin if (length (:new.isbn) = 10) :new.isbn := substr (:new.isbn, 1, 9); :new.isbn := concat (978, :new.isbn); :new.isbn := concat (:new.isbn, mod ((10 - mod ((to_number (substr (:new.isbn, 1, 1)) + 3 * to_number (substr (:new.isbn, 2, 1)) + to_number (substr (:new.isbn, 3, 1)) + 3 * to_number (substr (:new.isbn, 4, 1)) + to_number (substr (:new.isbn, 5, 1)) + 3 * to_number (substr (:new.isbn, 6, 1)) + to_number (substr (:new.isbn, 7, 1)) + 3 * to_number (substr (:new.isbn, 8, 1)) + to_number (substr (:new.isbn, 9, 1)) + 3 * to_number (substr (:new.isbn, 10, 1)) + to_number (substr (:new.isbn, 11, 1)) + 3 * to_number (substr (:new.isbn, 12, 1))), 10)), 10)); end if; end;
but gives following error:
error(5,5): pl/sql: statement ignored error(5,63): pls-00330: invalid use of type name or subtype name
i think did step 3 wrong (formula part)
function convert
used converting different character types each other. convert character string number use function to_number
. need replace in code convert (int, ...)
to_number()
function:
create or replace trigger isbnformatchange before insert on books each row begin if (length (:new.isbn) = 10) :new.isbn := substr (:new.isbn, 1, 9); :new.isbn := concat (978, :new.isbn); :new.isbn := concat (:new.isbn, mod ((10 - mod ((to_number(substr (:new.isbn, 1, 1)) + 3 * to_number(substr (:new.isbn, 2, 1)) + to_number(substr (:new.isbn, 3, 1)) + 3 * to_number(substr (:new.isbn, 4, 1)) + to_number( substr (:new.isbn, 5, 1)) + 3 * to_number( substr (:new.isbn, 6, 1)) + to_number(substr (:new.isbn, 7, 1)) + 3 * to_number( substr (:new.isbn, 8, 1)) + to_number( substr (:new.isbn, 9, 1)) + 3 * to_number( substr (:new.isbn, 10, 1)) + to_number(substr (:new.isbn, 11, 1)) + 3 * to_number( substr (:new.isbn, 12, 1))), 10)), 10)); end if; end; /
convert
function in documentation: http://docs.oracle.com/cd/b28359_01/server.111/b28286/functions027.htm#sqlrf00620
to_number
function in documentation: http://docs.oracle.com/cd/e11882_01/server.112/e41084/functions211.htm#sqlrf06140
Comments
Post a Comment