PL / SQL:更改插入数据时触发错误

时间:2015-03-28 11:07:52

标签: sql plsql triggers

我试图为书桌编写触发器。对于此表,

create table books (
    isbn VARCHAR(13) CHECK (LENGTH (isbn) = 10 or LENGTH (isbn) = 13),
    ...
    PRIMARY KEY (isbn)
);

我想写一个触发器,当插入长度为10的isbn时,根据以下规则将其格式更改为13位数:

  1. 删除最后一位数字
  2. 在前面添加了978
  3. 添加了由公式计算的检查位

    CHECK_BIT =(10 - (x1 + 3 * x2 + x3 + 3 * x4 + ... + x11 + 3 * x12)mod 10)mod 10

  4. 代码:

    create or replace trigger isbnFormatChange
    before insert on books
    for each row
    begin
      if (length (:new.isbn) = 10) then
        :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;
    

    但它会出现以下错误:

    Error(5,5): PL/SQL: Statement ignored
    Error(5,63): PLS-00330: invalid use of type name or subtype name
    

    我认为我做错了第3步(公式部分)

1 个答案:

答案 0 :(得分:1)

函数convert用于将不同的字符类型相互转换。要将字符串转换为数字,请使用函数to_number。因此,您只需要在代码中将所有convert (int, ...)替换为to_number()函数:

create or replace trigger isbnFormatChange
before insert on books
for each row
begin
  if (length (:new.isbn) = 10) then
    :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功能:http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions027.htm#SQLRF00620
文档中的to_number函数:http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions211.htm#SQLRF06140