【问题标题】:Trigger function not working/ SQL statement ignored触发功能不工作/ SQL 语句被忽略
【发布时间】:2015-10-22 19:02:10
【问题描述】:

我正在为学校开发一个数据库,除了触发器之外一切正常。 例如:

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:

【问题讨论】:

  • GAMECATEGORY是什么实体?
  • 这是它使用的表:CREATE TABLE GAMECATEGORY ( Category_ID number(13) primary key, CategoryName varchar2(50) not null, ViewerCount number(13) not null );
  • 您可以将序列分配简化为:new.Stream_ID := LIVESTREAM_Stream_ID_SEQ.nextval

标签: sql oracle triggers


【解决方案1】:

查看新的应该是:new。

【讨论】:

  • 我改变了它,但它编译了一些代码。仍然有正在编译但出现错误的触发器?这些错误表明 SQL 语句被忽略
  • 还有更多信息(上一行还是下一行?)?或者只忽略 SQL 语句。
  • 将在上面的 headanswer 中显示创建表、触发器和表的顺序
  • 它给出了这个问题,如问题编辑:ORA-00947
  • 值部分应该看起来像 VALUES ('A','B','C') 而不是 VALUES ('A,B,C')
【解决方案2】:

原因是您要插入整个字符串:'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;
  /

【讨论】:

  • 序列赋值可以简化为:new.Stream_ID := LIVESTREAM_Stream_ID_SEQ.nextval;
  • 我应用了它@a_horse_with_no_name。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 1970-01-01
  • 1970-01-01
  • 2017-09-22
  • 2012-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多