我尝试使用以下代码创建触发器。
CREATE OR REPLACE TRIGGER MYTABLE_TRG
BEFORE INSERT ON MYTABLE
FOR EACH ROW
BEGIN
select MYTABLE_SEQ.nextval into :new.id from dual;
END;
我收到错误
Error(2,52): PLS-00049: bad bind variable 'NEW.ID'
有什么想法吗?感谢。
答案 0 :(得分:14)
似乎错误代码告诉你表格中没有这样的列ID ......
答案 1 :(得分:1)
不知何故,您的环境将您的代码视为SQL而不是DDL语句。这对我有用(从命令提示符下运行sqlplus.exe):
SQL> create sequence mytable_seq;
Sequence created.
SQL> create table mytable (id number);
Table created.
SQL> CREATE OR REPLACE TRIGGER MYTABLE_TRG
2 BEFORE INSERT ON MYTABLE
3 FOR EACH ROW
4 BEGIN
5 select MYTABLE_SEQ.nextval into :new.id from dual;
6 END;
7 /
Trigger created.
请注意尾部的“/” - 这可能在您使用它编译的应用程序中很重要。
答案 2 :(得分:0)
如果有人使用适当的命名约定来识别这种类型 错误会更容易(正确的意思是使用前缀和后缀) 用于更好地暗示其目的的通用对象名称 即这样的事情会发现correct答案
--START -- CREATE A SEQUENCE
/*
create table "TBL_NAME" (
"TBL_NAME_ID" number(19,0) NOT NULL
, ...
*/
--------------------------------------------------------
-- drop the sequence if it exists
-- select * from user_sequences ;
--------------------------------------------------------
declare
c int;
begin
select count(*) into c from user_sequences
where SEQUENCE_NAME = upper('SEQ_TBL_NAME');
if c = 1 then
execute immediate 'DROP SEQUENCE SEQ_TBL_NAME';
end if;
end;
/
CREATE SEQUENCE "SEQ_TBL_NAME"
MINVALUE 1 MAXVALUE 999999999999999999999999999
INCREMENT BY 1 START WITH 1
CACHE 20 NOORDER NOCYCLE ;
-- CREATE
CREATE OR REPLACE TRIGGER "TRG_TBL_NAME"
BEFORE INSERT
ON "TBL_NAME"
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
DECLARE
tmpVar NUMBER;
BEGIN
tmpVar := 1 ;
SELECT SEQ_TBL_NAME.NEXTVAL INTO tmpVar FROM dual;
:NEW.TBL_NAME_ID := tmpVar;
END TRG_TBL_NAME;
/
ALTER TRIGGER "TRG_TBL_NAME" ENABLE;
-- STOP -- CREATE THE TRIGGER
答案 3 :(得分:0)
如果您像我一样并且您的代码应该可以工作,请在重新创建触发器之前尝试将其明确删除。愚蠢的甲骨文。