How to create something to update end date by starting day and duration column automaticly SQL

Potato

I want to create something that will update my data in table automaticly but i don't know what to use.

i have nr1 table

create table NR1
(
  id         INTEGER not null,
  price      INTEGER,
  price2     INTEGER,
  start_date DATE,
  end_date   DATE,
  duration   NUMBER
)

and i tried to create trigger which will update always after inserting or updateing my table, end_date, so i tried something like this:

create or replace trigger update_nr1_date
after update or insert on nr1
for each row
 when (new.id>0)
declare
begin
  UPDATE nr1
  set nr1.end_date =add_months(nr1.start_date, nr1.duration);
end;

but it have mutation problems, and i read something about that, and i udersatnd the concept of this. I want to make trigger like this (not sheduler, because i want to get it automaticly after inserting or updating some rows). Is it possible while inserting data to table it itself refill missing columns?

Alex Poole

Your trigger needs to before update in order to change the value in the row the trigger is fired again; and it needs to modify the current row's (new) pseudorecord via an assignment statement - you should not be issuing a separate update statement inside the trigger. It will cascade, for a start, and in your example woudl try to update every row in the table. But that is also causing the mutating table error - you're trying to update rows in the table the trigger is against. There are workarounds when that is actually necessary, but it isn't here, that update should just not be there.

So you would do:

create or replace trigger update_nr1_date
before update or insert on nr1
for each row
when (new.id>0)
begin
  :new.end_date := add_months(:new.start_date, :new.duration);
end;
/

But if you're on 11g or higher you can use a virtual column instead, without needing a trigger at all:

create table NR1
(
  id         INTEGER not null,
  price      INTEGER,
  price2     INTEGER,
  start_date DATE,
  end_date   DATE generated always as (add_months(start_date, duration)) virtual,
  duration   NUMBER
)

Then when you insert, skip that column in the statement:

insert into nr1 (id, price, price2, start_date, duration)
values (1, 2, 3, date '2018-06-01', 3);

1 row inserted.

select * from nr1 where id = 1;

        ID      PRICE     PRICE2 START_DATE END_DATE     DURATION
---------- ---------- ---------- ---------- ---------- ----------
         1          2          3 2018-06-01 2018-09-01          3

The end date always reflects the values of the other two columns.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Update end date by adding duration to start date SQL

From Dev

How to create day start and end date in PL SQL

From Dev

How to update mysql date column field with the next working day.?

From Dev

How to get start date and end date from the given date column according to task completion in a day (oracle)

From Dev

Sql server Varchar DateTime Column add Day to Date how to?

From Dev

How to format date column to get the day name in Oracle SQL?

From Dev

How to create a Date in SQL Server given the Day, Month and Year as Integers

From Dev

Mysql - how to calculate end date by using start date and duration

From Dev

Python - How to get a column 'duration' with start and end time column in csv?

From Dev

SQL help! How to create a new column that shows the time duration between two different periods

From Dev

How to update a date counter when starting activity

From Dev

how to create or update a column

From Dev

Update only day portion of SQL Date

From Dev

How to convert start/end date to week duration in Excel?

From Dev

How to update a date column with sysdate including timestamp in a dynamic sql

From Dev

How we find Latest Date value and update in another column in Sql?

From Dev

How to update a date column with sysdate including timestamp in a dynamic sql

From Dev

How to update a column name which is named after a date in SQL

From Dev

How to update date column in SQL from jdbcTemplate dynamically?

From Dev

How to get starting date and end date of year in Android?

From Dev

How to update date in sql

From Dev

how to convert day to date in sql?

From Dev

How to calculate start of day and end of day from a date in c#

From Dev

How to update a column with a date in Oracle?

From Dev

How to retrieve all the rows with date starting the first day of a month

From Dev

SQL Sum duration by hour and day

From Dev

Find start date based upon End Date and Duration - T-SQL

From Dev

Find start date based upon End Date and Duration - T-SQL

From Dev

Create start and end date from single date column

Related Related

  1. 1

    How to Update end date by adding duration to start date SQL

  2. 2

    How to create day start and end date in PL SQL

  3. 3

    How to update mysql date column field with the next working day.?

  4. 4

    How to get start date and end date from the given date column according to task completion in a day (oracle)

  5. 5

    Sql server Varchar DateTime Column add Day to Date how to?

  6. 6

    How to format date column to get the day name in Oracle SQL?

  7. 7

    How to create a Date in SQL Server given the Day, Month and Year as Integers

  8. 8

    Mysql - how to calculate end date by using start date and duration

  9. 9

    Python - How to get a column 'duration' with start and end time column in csv?

  10. 10

    SQL help! How to create a new column that shows the time duration between two different periods

  11. 11

    How to update a date counter when starting activity

  12. 12

    how to create or update a column

  13. 13

    Update only day portion of SQL Date

  14. 14

    How to convert start/end date to week duration in Excel?

  15. 15

    How to update a date column with sysdate including timestamp in a dynamic sql

  16. 16

    How we find Latest Date value and update in another column in Sql?

  17. 17

    How to update a date column with sysdate including timestamp in a dynamic sql

  18. 18

    How to update a column name which is named after a date in SQL

  19. 19

    How to update date column in SQL from jdbcTemplate dynamically?

  20. 20

    How to get starting date and end date of year in Android?

  21. 21

    How to update date in sql

  22. 22

    how to convert day to date in sql?

  23. 23

    How to calculate start of day and end of day from a date in c#

  24. 24

    How to update a column with a date in Oracle?

  25. 25

    How to retrieve all the rows with date starting the first day of a month

  26. 26

    SQL Sum duration by hour and day

  27. 27

    Find start date based upon End Date and Duration - T-SQL

  28. 28

    Find start date based upon End Date and Duration - T-SQL

  29. 29

    Create start and end date from single date column

HotTag

Archive