触发器功能不起作用/ SQL语句被忽略

时间:2015-10-22 19:02:11

标签: sql oracle triggers

我在学校的数据库上工作,一切都在工作,除了触发器。 例如:

create or replace TRIGGER T_CATEGORY
  before insert on GAMECATEGORY
  for each row
BEGIN 
  select G_Category_ID_SEQ.nextval into new.Category_ID from dual;
END;

它是否忽略了SQL语句并且没有创建触发器? 有没有人可以帮助我? :d

:编辑

CREATE TABLE LIVESTREAM
(
Stream_ID                    number(13),
LivestreamURL                varchar2(50),
primary key(Stream_ID, LivestreamURL),
Channel_ID                   number(13) not null,
Category_ID                 number(13) not null,
Title                       varchar2(50) not null,              
Organisation_Name           varchar2(50),
Viewers                     number(13) not null,
LivestreamStatus            varchar2(10)   check (UPPER(LivestreamStatus) IN ('ONLINE','OFFLINE')),
Followers                   number(13) not null,
"Views"                     number(13) not null

);
ALTER TABLE LIVESTREAM ADD constraint FK_LIVESTREAM_Category_ID foreign key(Category_ID) REFERENCES GAMECATEGORY(Category_ID)on delete cascade;
ALTER TABLE LIVESTREAM ADD constraint FK_LIVESTREAM_Channel_ID foreign key(Channel_ID) REFERENCES USERCHANNEL(Channel_ID)on delete cascade;


CREATE SEQUENCE LIVESTREAM_Stream_ID_SEQ
  start with 1
  increment by 1;

CREATE OR REPLACE TRIGGER T_LIVESTREAM
  before insert on LIVESTREAM
  for each row
BEGIN 
  select LIVESTREAM_Stream_ID_SEQ.nextval into :new.Stream_ID from dual;
END;
/

当我将数据插入表格时,它会给我这个错误:

INSERT INTO LIVESTREAM(LivestreamURL, Channel_ID, Category_ID, Title, Organisation_Name, Viewers, LivestreamStatus, Followers, "Views")
VALUES('http://www.twitch.tv/nightblue3, 2, 1,Next Stream: Friday @ 4 AM PST / 7 AM EST / NOON GMT, The Round Table, , OFFLINE, 1052215, 115257581')
Error at Command Line : 253 Column : 1
Error report -
SQL Error: ORA-00947: not enough values
00947. 00000 -  "not enough values"
*Cause:    
*Action:

2 个答案:

答案 0 :(得分:0)

查看新的应该是:new。

答案 1 :(得分:0)

原因是你要插入整个字符串:'http://www.twitch.tv/nightblue3, 2, 1,Next Stream: Friday @ 4 AM PST / 7 AM EST / NOON GMT, The Round Table, , OFFLINE, 1052215, 115257581' 单独进入LivestreamURL专栏。您必须将查询更改为:

  INSERT INTO LIVESTREAM(LivestreamURL, 
        Channel_ID, Category_ID, 
        Title, Organisation_Name, 
        Viewers, LivestreamStatus, 
        Followers, "Views")
  VALUES
     ('http://www.twitch.tv/nightblue3', 
        2, 1,
        'Next Stream: Friday @ 4 AM PST / 7 AM EST / NOON GMT', 
        'The Round Table', , 
        'OFFLINE', 1052215,
        115257581)

然后将您的触发器更改为:

  CREATE  TRIGGER T_LIVESTREAM
    before insert on LIVESTREAM
    for each row
  BEGIN 
     if :new.Stream_ID is null then
    :new.Stream_ID := LIVESTREAM_Stream_ID_SEQ.nextval;
    end if;
  END;
  /

  create or replace TRIGGER T_CATEGORY
     before insert on GAMECATEGORY
     for each row
  BEGIN 
     if :new.Category_ID is null then
     :new.Category_ID := G_Category_ID_SEQ.nextval;
     end if;
  END;
  /