Adding (count) sequence value to column value via trigger in Oracle SQL - sql

I would like to write a trigger which will be able to add sequence value to column value.
Example: Sequence value is 5 and column value in table is 45. Result after inserting new row should be 50.
Problem is that I dont know how to fetch value from the column and then add it to the sequence value in trigger code.
After using my trigger I am able to insert a new row which will create columns with value = 5.
CREATE SEQUENCE Adding_Seq
START WITH 5
INCREMENT BY 1;
CREATE OR REPLACE TRIGGER Adding_Trigger
BEFORE INSERT ON Table
FOR EACH ROW
BEGIN
SELECT Adding_Seq.nextval
INTO :new.Value
FROM dual;
END;
I tried to add some "+" in section SELECT or later in INTO with name of the column from table, but there were errors.
Is is possible to do something like this in trigger or not?
Thanks for you help!

This is really strange. But you can just use addition:
BEGIN
SELECT :new.Value + Adding_Seq.nextval INTO :new.Value
FROM dual;
END;

Related

Values of the inserted row in a Trigger Oracle

I want a trigger that updates the value of a column, but I just want to update a small set of rows that depends of the values of the inserted row.
My trigger is:
CREATE OR REPLACE TRIGGER example
AFTER INSERT ON table1
FOR EACH ROW
BEGIN
UPDATE table1 t
SET column2 = 3
WHERE t.column1 = :new.column1;
END;
/
But as I using FOR EACH ROW I have a problem when I try it, I get the mutating table runtime error.
Other option is not to set the FOR EACH ROW, but if I do this, I dont know the inserted "column1" for comparing (or I dont know how to known it).
What can I do for UPDATING a set of rows that depends of the last inserted row?
I am using Oracle 9.
You should avoid the DML statements on the same table as defined in a trigger. Use before DML to change values of the current table.
create or replace trigger example
before insert on table1
for each row
begin
:new.column2 := 3;
end;
/
You can modify the same table with pragma autonomous_transaction:
create or replace trigger example
after insert on table1 for each row
declare
procedure setValues(key number) is
pragma autonomous_transaction;
begin
update table1 t
set column2 = 3
where t.column1 = key
;
end setValues;
begin
setValues(:new.column1);
end;
/
But I suggest you follow #GordonLinoff answere to your question - it's a bad idea to modify the same table in the trigger body.
See also here
If you need to update multiple rows in table1 when you are updating one row, then you would seem to have a problem with the data model.
This need suggests that you need a separate table with one row per column1. You can then fetch the value in that table using join. The trigger will then be updating another table, so there will be no mutation problem.
`create table A
(
a INTEGER,
b CHAR(10)
);
create table B
(
b CHAR (10),
d INTEGER
);
create trigger trig1
AFTER INSERT ON A
REFERENCING NEW AS newROW
FOR EACH ROW
when(newROW.a<=10)
BEGIN
INSERT into B values(:newROW.b,:newROW.a);
END trig1;
insert into A values(11,'Gananjay');
insert into A values(5,'Hritik');
select * from A;
select * from B;`

how auto generate number start from specific number the first 4 # include the current year in oracle 11g

i have table which have id when iwnna to insert new record its by default generate new # increment by 1 how can i do it please
the number is begin with year and serial for ex; 20130001,20130002,20130003 and so on , when the year is end then will start 20140001,20140002,20140003
Putting aside the question why you would want to do this.
The most straightforward approach is to create an Oracle SEQUENCE object, starting at the value you want to start with, increment of 1. As an example:
CREATE SEQUENCE myseq START WITH 20130001 INCREMENT BY 1 NOCACHE ;
To make use of the sequence object to supply a value for a table on an INSERT, create a BEFORE INSERT trigger
CREATE TRIGGER mytable_trg_bi
BEFORE INSERT ON mytable
FOR EACH ROW
BEGIN
IF NEW.mycol IS NULL THEN
SELECT myseq.NEXTVAL FROM DUAL INTO NEW.mycol;
END IF;
END;
/
(It's been a while since I've worked with the Oracle syntax; some of the keywords might not be in the right order. But this is the normal pattern for assigning unique, system generated values to a column on an INSERT.
That part is easy.
The trickier part is getting the sequence to "jump" to a specific value.
There's a couple of approaches to doing that. One would be drop the sequence object and re-create a new object, with the same name, with a new starting value. But that's not very elegant, and fairly disruptive.
Another approach is to modify the increment value, select nextval to make it jump, and then set the increment back to 1.
As a rough outline of what that might look like:
DECLARE
ln_val NUMBER;
BEGIN
-- retrieve next value from sequence
SELECT myseq.NEXTVAL FROM DUAL INTO ln_val;
-- set increment so next call to nextval will "jump"
EXECUTE IMMEDIATE
'ALTER SEQUENCE myseq INCREMENT BY '|| 20140001 - 2 - ln_val ||' NOCACHE';
-- this should return us 20140000
SELECT myseq.NEXTVAL FROM DUAL INTO ln_val;
-- reset increment back to 1
EXECUTE IMMEDIATE
'ALTER SEQUENCE myseq INCREMENT BY 1';
END;
/
Note that this approach of setting/resetting the current value of the sequence is subject to a race condition, if another session is pulling values from the SEQUENCE at the same time.

PLS-00103: Encountered the symbol "¿"

I am trying to make sure that my primary key auto increments. The code below is what I have tried so far.
create or replace trigger field_null
before insert on table
for each row
begin
if :new.number_id is null then
select number_id_SEQ.nextval into :new.number_id from table;
end if;
end;​
Instead of table in select query,try to use dual. Try this General Trigger syntax to create trigger for Auto-increment column
CREATE OR REPLACE TRIGGER %triggername%
BEFORE INSERT ON %tablename% FOR EACH ROW
BEGIN
SELECT %seqname%.NEXTVAL
INTO :NEW.%columnname%
FROM DUAL;
END;
%seqname% will be replaced with the name of the sequence.
%triggername% will be replaced with the name of the trigger.
%columnname% will be replaced with the name of the associated column.
And to Create Sequence, You can use the following syntax:-
CREATE SEQUENCE %seqname%
START WITH 1
INCREMENT BY 1;
Refer Here

how to write Instead of update? - Trigger

I have table A and there is a column name COL_A.
I want that if someone change the value, lets say from 1 to 'X' (not costant) that the trigger will change it back from 'X' to 1.
SQLite does not support changing the new column values.
The only way to change a column in a trigger would be to run an UPDATE command,
but that would run the trigger again.
What you can do is to prevent changing the column in the first place:
CREATE TRIGGER IF NOT EXISTS prevent_col_a_change
BEFORE UPDATE OF col_a ON a
BEGIN
SELECT RAISE(ABORT, 'COL_A must not be changed');
END;
UPDATE trigger is a good solution for your case. Just set old value to the new value, that will lead to behaviour you want.
For example:
CREATE OR REPLACE TRIGGER orders_before_update
BEFORE UPDATE
ON orders
FOR EACH ROW
BEGIN
:new.CreatedAt:= :old.CreatedAt;
END;

Behaviour of insertion trigger when defining autoincrement in Oracle

I have been looking for a way to define an autoincrement data type in Oracle and have found these questions on Stack Overflow:
Autoincrement in Oracle
Autoincrement Primary key in Oracle database
The way to use autoincrement types consists in defining a sequence and a trigger to make insertion transparent, where the insertion trigger looks so:
create trigger mytable_trg
before insert on mytable
for each row
when (new.id is null)
begin
select myseq.nextval into :new.id from dual;
end;
I have some doubts about the behaviour of this trigger:
What does this trigger do when the supplied value of "id" is different from NULL?
What does the colon before "new" mean?
I want the trigger to insert the new row with the next value of the sequence as ID whatever the supplied value of "new.id" is. I imagine that the WHEN statement makes the trigger to only insert the new row if the supplied ID is NULL (and it will not insert, or will fail, otherwise).
Could I just remove the WHEN statement in order for the trigger to always insert using the next value of the sequence?
The WHEN condition specifies a condition that must be TRUE for the trigger to fire. In this exampple, the trigger will only fire if the new row has a NULL IS. When the ID is not null, the trigger will not fire and so whatever value ID has been given in the insert statement will be left alone.
Yes, if you simply remove the WHEN condition then the trigger will always fire and so will always provide a sequence value for ID.
Nothing. That allows to specify a value manually.
It's a placeholder for the new value of the column.
You have 2 methods you can do:
if the table looks like this:
create table my_test (
id number,
my_test data varchar2(255)
);
and your sequence is this:
create sequence test_seq
start with 1
increment by 1
nomaxvalue;
you can create a trigger like this (with no When statement like Tony Andrews said)
create trigger test_trigger
before insert on my_test
for each row
begin
select test_seq.nextval into :new.id from dual;
end;
or you could just simply use this then you don't need a trigger:
insert into my_test values(test_seq.nextval, 'voila!');