I have problem when trying to excute a sequence with a before insert trigger.
CREATE TABLE personne (ID number, nom varchar2(250 char));
CREATE SEQUENCE s_inc_pers START WITH 1 INCREMENT BY 1;
CREATE TRIGGER tr_inc_pers ON t1 BEFORE INSERT
FOR EACH ROW
DECLARE
BEGIN
select s_inc_pers into :new.t1.ID from DUAL;
END.
Oracle reports a bad bind variable if you try to reference something with :NEW that is not a column in the target table. In this case it may be prompting you for a bind value as the statement is malformed.
The NEW pseudorecord refers to the row in the triggering table that is being affected by the statement. You don't need to (and mustn't) include the table name when you use the pseudorecord field, so :new.t1.ID should just be :new.ID.
To get the next value from the sequence you need to use nextval, you can't only provide the sequence name.
Your clauses are also in the wrong order, you need the DML event (insert) befroe the target table.
CREATE TRIGGER tr_inc_pers BEFORE INSERT ON t1
FOR EACH ROW
BEGIN
select s_inc_pers.nextval into :new.ID from DUAL;
END;
As you are using 11g you don't even need to select from dual, you can just assign the column value:
CREATE TRIGGER tr_inc_pers BEFORE INSERT ON t1
FOR EACH ROW
BEGIN
:new.ID := s_inc_pers.nextval;
END;
I am having an issue when creating the auto increment trigger in Oracle 11g. If someone can point out what I am doing wrong, I would really appreciate it. My script for the sequence is this :
CREATE SEQUENCE SPSS_QUOTE_LINE_ITEMS_SEQ start with 1
increment by 1
minvalue 1;
The script for trigger:
CREATE OR REPLACE TRIGGER SPSSQUOTELINEITEMS_ON_INSERT
BEFORE INSERT ON SPSS_QUOTE_LINE_ITEMS
FOR EACH ROW
BEGIN
SELECT SPSS_QUOTE_LINE_ITEMS_SEQ.NEXTVAL
INTO :new.line_num
FROM dual;
END;
The error I am getting:
[Code: 900, SQL State: 42000] ORA-00900: invalid SQL statement
Thanks a lot.
The correct way of doing this to modify your trigger as below:
CREATE OR REPLACE TRIGGER SPSSQUOTELINEITEMS_ON_INSERT
BEFORE INSERT ON SPSS_QUOTE_LINE_ITEMS
FOR EACH ROW
BEGIN
:new.line_num := SPSS_QUOTE_LINE_ITEMS_SEQ.NEXTVAL;
--No need to write any Select Into statement here.
END;
Or if i follow your way then it goes like
CREATE OR REPLACE TRIGGER SPSSQUOTELINEITEMS_ON_INSERT
BEFORE INSERT ON SPSS_QUOTE_LINE_ITEMS
FOR EACH ROW
declare
var number;
BEGIN
SELECT SPSS_QUOTE_LINE_ITEMS_SEQ.NEXTVAL
INTO var
FROM dual;
:new.line_num :=var;
END;
You normally use the terms in a trigger using :old to reference the old value of the column and :new to reference the new value.
Im trying to execute a block in PL/SQL that
Get the actual data of a row by doing table%rowtype
Modify an attribute from it
And then inserting the record in a INSERT INTO...VALUES statement like follows.
declare
v_record table%ROWTYPE
begin
select *
into v_record
from X_table;
insert into X_table values (v_record)
end;
But the error that raises when I do this is PL/SQL: ORA-00947: not enough values
I've solved this issue removing the parenthesis from the values selection in the INSERT statement.
For example:
insert into X_table values v_record;
I am trying to write a stored procedure that selects indiv_ids and transaction_ids and inserts these into a table on my schema. In doing so, I want to pass in variables and have the stored procedure use if statements to select the indiv_ids and transaction_ids from different tables depending on the information passed in. I've tried a few variations and can't get the procedure to work without an error. Thanks!
create or replace procedure myproc (name_type in varchar2, dept in number)
is begin
if name_type='promo' then insert into mytable(indiv_id,transaction_id)
---sql here;
commit;
elsif name_type='deal' then insert into mytable(indiv_id, transaction_id)
---sql here;
commit;
end if;
end;
errors: Error(8,10): PL/SQL: SQL Statement ignored,
Error(16,31): PL/SQL: ORA-00942: table or view does not exist
Note that name_type variable is declared as NUMBER, but in the procedure body it is compared with strings:
if name_type='promo' then
This is not the cause of ORA-00942 error, but anyway it makes your procedure not working correctly.
I have the following procedure
create or replace
procedure prod_add_sp
(p_idproduct in bb_product.idproduct%type,
p_prodname in bb_product.productname%type,
p_descrip in bb_product.description%type,
p_prodimage in bb_product.productimage%type,
p_prodprice in bb_product.price%type,
p_prodactive in bb_product.active%type)
is
begin
insert into bb_product(idproduct,productname,description,productimage,price,active)
values (p_idproduct,p_prodname,p_descrip,p_prodimage,p_prodprice,p_prodactive);
commit;
end;
How do I modify the above with the seq.nextval portion so when executed a new row is inserted with a unique primary key? IDPRODUCT is a primary key so it is required.
Create a sequence with any name say nextID.
Now use below code:
Create a sequence with any name say nextID.
Now use below code:
create or replace procedure prod_add_sp (p_idproduct in bb_product.idproduct%type, p_prodname in bb_product.productname%type, p_descrip in bb_product.description%type, p_prodimage in bb_product.productimage%type, p_prodprice in bb_product.price%type, p_prodactive in bb_product.active%type) is
begin
insert into bb_product(idproduct,productname,description,
productimage,price,active)
values (nextID.nextVal,p_prodname,p_descrip,
p_prodimage,p_prodprice,p_prodactive);
commit;
end;
You need to create a sequence first i.e.:
CREATE SEQUENCE productsID_seq
START WITH 0
INCREMENT BY 1
NOMAXVALUE;
And then in the values (... line:
insert into bb_product(idproduct,productname,description,productimage,price,active)
values (productsID_seq.nextval,...
Here's some good info from the Oracle DB Docs
You will have to create SEQUENCE first. Then you can use sequencename.nextval in the idproduct column.