Insert data into the same table based on certain conditions

Hello. I'm new to this forum.
I have to write a stored procedure to insert data in a table MYTABLE say, having a structure like:

Col1 Col2 Col3... TotalInstallments CurrentInstallment PaidAmount MonthYear
I have to insert all the data that it is in the same table (MYTABLE) except change some fields based on certain conditions:

1. if PaidAmount > 0 & & CurrentInstallment < TotalInstallment then

CurrentInstallment = CurrentInstallment + 1

2. in the field MonthYear I have data ex. 01/2012, 11/2012 formate(month/year)...

So, I have to insert data by incrementing the month and year. for example:

If currentdata is 11/2012 next data will be 12/2012

But following will be 01, 2013
I have to select all records that belongs to the previous month (across the field MonthYear) and put the audit on each record selected and insert data and then turns them into table (MYTABLE) even.

How to achieve that?

Thank you.

978184 wrote:
Hello. I'm new to this forum.
I have to write a stored procedure to insert data in a table MYTABLE say, having a structure like:

Col1 Col2 Col3... TotalInstallments CurrentInstallment PaidAmount MonthYear
I have to insert all the data that it is in the same table (MYTABLE) except change some fields based on certain conditions:

1. if PaidAmount > 0 & CurrentInstallment

CurrentInstallment = CurrentInstallment + 1

2. in the field MonthYear I have data ex. 01/2012, 11/2012 formate(month/year)...

So, I have to insert data by incrementing the month and year. for example:

If currentdata is 11/2012 next data will be 12/2012

But following will be 01, 2013
I have to select all records that belongs to the previous month (across the field MonthYear) and put the audit & on each of the selected data record and insert then turns them into table (MYTABLE).

You can do this way:

This is not tested, but if you can provide the example of table structure and data (IN create table and insert scripts), it can be put to the test.

insert into your_table
(col1, col2, col3...current_installment, month_field)
select col1, col2, col3..,
       current_installment +
       case when paidamount > 0 and current_installment < total_installment then
        row_number() over (
                            partition by column1, column2,.. columnn      -->You may choose partition if you want the
                                                                          --Increment of Current_installment to reset after particular combination ends
                            order by primary_key        -->Order the Increment, you may choose to add more columns to order by
                          )
      else
        0                                               --> if condition is not met, then Add 0
      end curr_installment,
      add_months(to_date(month_field, 'MM/YYYY'), 1) nxt_month
  from your_table;

Tags: Database

Similar Questions

  • cannot insert data into the PRODUCT_USER_PROFILE table

    I've connected to the database as the sysdba, which is installed on VMWARE. database is oracle 11g.
    whenever I insert data in the table PRODUCT_USER_PROFILE that the database returns 1 row inserted and then when I try to show everything on the table before or after the statement commit is made the database returns "No. LINES SELECTED.

    guys any idea about this problem...

    Hello

    Try to connect as a system and make the insert and check. Always think about the issue.

    -Pavan Kumar N
    Oracle 9i / 10g - OCP
    http://oracleinternals.blogspot.com/

    Published by: pounet on January 4, 2010 16:29

  • Insert data into the source of destinator Table

    Hi all

    Need to insert data in sample_table1 table xxc_source_table sample_table2

    create table xxc_source_table (DESCRIPTION varchar2 (10));

    Insert the table xxc_source_table values('A201.) ABC.4084.GR');

    create table sample_table1 (col_1 varchar2 (10), col_2 varchar2 (10), col_3 varchar2 (10), col_4 varchar2 (10));

    create table sample_table2 (col_1 varchar2 (10), col_2 varchar2 (10), col_3 varchar2 (10), col_4 varchar2 (10), moved_flag varchar2 (2));

    col_1 = A201

    col_2 = ABC

    col_3 = 4084

    col_4 = GR

    Note: Insert data into the col_1, the col_3, the col_4 of the xxc_source_table sample_table1

    (2) if the next (form xxc_source_table) data is exist in the sample_table2, and then set the moved_flag as Y in this column

    3) xxc_source_table has 17000 lines

    Thank you.

    Post edited by: Rajesh123 please do not consider cross the line message

    Hi Renon,

    Why you don't want substr and instr? For best performance, you should go with substr and instr instead of regexp_substr. However you asked me to provide the code instead of substr and InStr. Then try the below...

    INSERT ALL

    IN VALUES sample_table1 (col1, col2, col3, col4)

    IN sample_table2 VALUES (col1, col2, col3, col4, 'Y')

    SELECT REGEXP_SUBSTR (DESCRIPTION,'[^.] +', 1, 1) col1,.

    REGEXP_SUBSTR (DESCRIPTION,'[^.] +', 1, 2) col2.

    REGEXP_SUBSTR (DESCRIPTION,'[^.] +' 1, 3) col3.

    REGEXP_SUBSTR (DESCRIPTION,'[^.] +' 1, 4) col4

    OF xxc_source_table;

    Thank you

    Ann

  • How to insert the legacy data into the QP_RLTD_MODIFIERS table?

    How insert Legacy data into the QP_RLTD_MODIFIERS table in the instance of R12.

    I would use the QP_Modifiers_PUB API. Process_Modifiers to push the old data on prices in R12.  QP_RLTD_MODIFIERS is only used for certain types of discounts (in my prod environment, only the promos are given in this table).

  • What trigger to use to insert data into the table db forms

    Hello

    My form is current having a block of database with the table reference. When you enter data into the form field and click Save. Automatically record is inserted in the database table.
    I want to do this under manual insert. I changed the block of data to a data non-base. Where should I write the insert statement to insert data into the table.
    Is - shutter release key of the form-level validation?

    Please notify.

    Thank you
    Didi.

    You can guide me how to remove the message FRM-40401 no changes to save and instead, I wanted to put 1 transaction record successful... some like it like that.

    Hi didi.
    And this is my last post I'll sleep. ;)
    Take form level trigger named preform
    and write on it

    :SYSTEM.MESSAGE_LEVEL := 5;
    

    I hope this works...

    Hamid

    Mark correct/good to help others to get the right answers. *

  • How to insert data into the table by using the expression builder in the assign activity

    How to insert data into the table by using the expression builder in affect business in BPEl, I use SOA Suite 11.1.1.5
    Can someone help me please

    Hello

    I don't think that oraext:query-database() can insert data into the table.

    What are your needs?
    Can not you plan to use the DB adapter with the insert operation?

    Kind regards
    Neeraj Sehgal

  • How to insert 10,000 records test data into the emp table

    Hi I am new to oracle can someone please help me write a program so that I can insert the test data into the emp table
    SELECT       LEVEL empno,
                 DBMS_RANDOM.string ('U', 20) emp_name,
                 TRUNC (DBMS_RANDOM.VALUE (10000, 100000), 2) sal,
                 DBMS_RANDOM.string ('U', 10) job,
                 TO_DATE ('1990-01-01', 'yyyy-mm-dd')
                 + TRUNC (DBMS_RANDOM.VALUE (1, 6000), 0)
                   hiredate,
                 CASE
                   WHEN LEVEL > 10 THEN TRUNC (DBMS_RANDOM.VALUE (1, 11), 0)
                   ELSE NULL
                 END
                   mgr,
                 TRUNC (DBMS_RANDOM.VALUE (1, 5), 0) deptno
    FROM         DUAL
    CONNECT BY   LEVEL <= 10000
    
  • How to insert data into the BLOB column

    Hi all

    Can someone help me to insert data in the BLOB data type column?

    The structure of the table is
    CREATE TABLE XXATFL_DM_FORCAST_STG
    (
    TASK_ID NUMBER,
    USER_ID NUMBER,
    CREATED_BY NUMBER (15),
    CREATION_DATE DATE,
    LAST_UPDATED_BY NUMBER (15),
    DATE OF LAST_UPDATE_DATE,
    LAST_UPDATE_LOGIN NUMBER (15),
    RECORD_STATUS VARCHAR2 (1 BYTE),
    ERROR_MESSAGE VARCHAR2 (4000 BYTE),
    DATA_FILE BLOB
    )

    I want to insert data into the column DATA_FILE. and this insert statement inside a procedure.

    Please help me as soon as possible because it is very urgent for me

    Thank you and best regards,
    Charrier

    Charrier,

    If you form the string yourself, you can use the function utl_raw.cast_to_raw on your channel.

    http://download.Oracle.com/docs/CD/B12037_01/AppDev.101/b10802/u_raw.htm#997086

    sql> create table t(
      2    id number,
      3    l_blob blob
      4  );
    
    sql> insert into t values(1, utl_raw.cast_to_raw('SampleString'));
    
    1 row created.
    
    sql> commit;
    
    Commit complete.
    
  • Cannot insert data into the database

    Hello world

    I stuck with a problem in DB juice. When I try to insert data into the database using DB tool, I get a repeated error message (error 1). Please find the my vifile below and solve say.

    Problem is use Labiew 8.2. So try to answer accordingly

    Try it with a cluster instead of a string or an array.

  • How to insert data into the database using smartview

    Hello
    I am trying to insert data into the database using * "Send data" * button on the Ribbon of Essbase.
    My database is empty.

    I opened an ad hoc network, it returns * "#missing" * in all cells
    I have modified the cells and provided data in the cells that I want to. Now, I supported on * "Send data" * button.
    It just reloaded the adhoc grid instead of submit data, I rechecked the data through data console Administrative Service are not inserted.

    I am following the right way to insert data? If not, could you please suggest me how (Populate) insert default data in the database?

    --
    VINET

    You go about it the right way, once you have submitted if you réactualisiez then data values should be there, if you POV is against members of dynamic calc and then data not written to the database, you need to check the Member properties of your POV.

    See you soon

    John
    http://John-Goodwin.blogspot.com/

  • ADF progress indicator to insert the data into the file table.

    I have a requirement where the progress bar should indicate the percentage of completion to insert the data into the tables of database files in a given folder path. I found an article that explains how to get the progress indicator when downloading a file in
    http://www.gebs.ro/blog/Oracle/Oracle-ADF-progress-indicator-in-Fusion-Middleware-11g/. I believe that I must first get the total number of files in the folder and then begin to compare with the number of files that are inserted into the table similar to what is done by downloading (getting the file size and download size), but I don't know how to do this. Can anyone help?

    May be that this example allows you to

    * 042.     Dynamically change the color of progress bar based on its current value *.

    http://www.Oracle.com/technetwork/developer-tools/ADF/learnmore/index-101235.html

  • Insert select on the same table: possible without side effects?

    I have a very large table T1 containing millions of records. I need to treat its lines and create a few new lines based on selection.

    Table T1 contains events and one of them, with the code 100, is created by the further development of other events inside the table.

    My code is as follows:

    insert /*+append */ into T1 (code,...) values (100, c1,c2,...)
    select c1,c2... from T1 where (code=20 or code=10) and <other conditions>...
    
    

    as you can see I'm extract T1 lines to insert again in T1 with a different code and I use the direct path in order to get good performance.

    My fear is: choose is made from the same table I risk data loss? In general it is a good practice? Or is it better to create another table?

    Hello

    No I don't think that there may be loss of data. But that may depend on the behavior of the application and your where clause.

    I will explain how it is treated, so you can see if it's ok:

    1. the table is locked because of the insert add, (as)

    2 lines are read by select and make compatible from the State that was at the beginning of the query - 1.

    3 rows are inserted at the end, after the high-water line

    4 columns for new lines are sorted to be merged in the index

    5. high watermark is adjusted - visible new lines and lock is released

    Note that 2. and 3. occur at the same time: rows are inserted all read.

    Note that anyone can choose in the table during the operation - they see the changes committed only - if the State 1.

    all other DML are waiting for the lock being released, and will see new ranks and then

    If you have things that prevent the direct-path insert, append the hint will be ignored. So, if you must rely on close to 1. then the best lock explicitly with table lock. But I don't think that you need.

    Kind regards

    Franck.

  • Inserting data in the nested table

    I explore the differences between the OBJECT and RECORD.

    As I'm still learning, I found that both are structures which essentially brings together different elements of types of different data or columns of data types, one is used in SQL and other is used in PL/SQL, please correct me if I'm wrong in my understanding.

    Below, I am trying to insert data into an array of type object, but I can't can you please help.

    CREATE OR REPLACE type sam as OBJECT
    (
    v1 NUMBER,
    v2 VARCHAR2(20 CHAR)
    );
    
    ---Nested Table---
    create or replace type t_sam as table of sam;
    
    --Inserting data----
    insert into table(t_sam) values(sam(10,'Dsouza'));
    

    Error message:

    Error starting at line 22 in command:
    insert into table(t_sam) values(sam(10,'Dsouza'))
    Error at Command Line:22 Column:13
    Error report:
    SQL Error: ORA-00903: invalid table name
    00903. 00000 -  "invalid table name"
    *Cause:    
    *Action:
    

    Ariean wrote:

    So the only purpose of the equivalent concept of SQL types of nested tables is to use one of the data types when you define an actual table?

    So-you can certainly use more that a simple "set an actual table. (I'm fairly certain that you can pass a table nested within a procedure, for example - try it, though - I'm not 100% sure on that - it just 'logic'.) If you can define a type, you can use it, pass it around, whatever.).

    Ariean wrote:

    And this nested table could be a record in SQL or object in PLSQL or simple data type (varchar number, etc.)?

    Nested tables are just like any other type of custom data. You can create a nested table of other types of data. You can create a custom of nested tables data type.

    Stupidly, he could become... uh, stupid O_0

    CREATE TYPE o_myobj1 AS object ( id1   number, cdate1  date );
    
    CREATE TYPE t_mytype1 AS table of o_myobj1;
    
    CREATE TYPE o_myobj2 AS object ( id2   number,  dumb  t_mytype1 );
    
    CREATE TYPE t_dumber AS table of o_myobj2;
    

    O_0

    OK, my brain is starting to hurt - I hope you get the idea

    Ariean wrote:

    Second is my understanding correct any OBJECT & RECORD?

    I see no benefit to describe another way.

  • Error inserting data into the database of JSF

    Hi all

    I have a form and a button of validation. I created the form using the editable view object created on an entity object. When the student has table (base table of data in which I have to enter data) was empty, I managed to insert the data into the table.when I run the same second time application it shows data that I entered previously. But iam able to modify this data and insert the new record again. What should I do so that it does not show the data I already inserted previously when I run again. I think that iam missing something. Please suggest.

    Thank you.

    Here are two ways to achieve the same thing:

    -If you use taskflows, you can add createInsert action as activity by default and then have a navigation from this at the registration page so that the creation form will be shown on the loading of the page. As a result, you can directly enter and validate.
    -If not, have binding action createInsert in your pagedef and add an executable invokeAction for the same thing with prepareModel so that the registration form empty appears when loading the page

    Jean Lou

  • Support for mandatory request - insert, select from the same table

    Hi all

    I need your help to write queries effectively.
    Oracle Version: 10.2.0.3.0
    OPERATING SYSTEM: UNIX

    I have a METRICS_TBL that is mentioned below table.

    CYCLE_DATE METRIC VALUE
    08/17/2008 COST-TV 100
    08/17/2008 COST-JOURNAL 50
    08/17/2008 COST-POSTALMAIL 25
    08/17/2008-PROD-TV 10
    08/17/2008-PROD-JOURNAL 25
    08/17/2008-PROD-POSTALMAIL 5

    Any data above, I have to add (Insert into select METRICS_TBL METRICS_TBL) at the same table with the records as mentioned below.

    2008-08-17 COSTPERPROD-TV 10
    08/17/2008-COSTPERPROD-LOG 2
    2008-08-17 COST PROD-POSTALMAIL 5

    Basically, I need to calculate the cost per product for each category. Depending on the settings available, metric should also be changed as COSTPERPROD and values should be cost/prod under each category.

    Can someone help me with the query.

    Thank you

    Something like this:

    INSERT INTO metrics_tbl
    (cycle_date, metrics, value)
    SELECT cost.cycle_date
             , 'COSTPERPROD-'||cost.mtype
             ,cost.value / prod.value
    FROM  (
       select cycle_date,substr(metrics, 1, 4) mtype, substrmetrics,instr(metrics,'-')+1) mmetric
       where substr(metrics, 1, 4) = 'PROD'
       ) prod
    INNER JOIN (   select cycle_date,substr(metrics, 1, 4) mtype, substrmetrics,instr(metrics,'-')+1) mmetric
       where substr(metrics, 1, 4) = 'COST'
       ) cost on cost.cycle_date = prod.cycle_date and cost.mmetric = prod.mmetric
    

    Ideally you would divide your METRICS column into two: one for the thing you're measuring (e.g. TV or NEWSPAPER) and the other for the metric (COST, PROD, COSTPERPROD etc.). That's what I did in the views online. Without it, it's kinda a mess and behave so, if the table becomes much more.

    HTH

    Nigel cordially

    Edited by: nthomas on January 14, 2009 15:57 - small correction SQL

Maybe you are looking for

  • New display driver for Satellite L755D necessary

    Hello What is the next round of pilots who come out to the ati display driver?Satellite L755D - APU A6 - 3400M 12th with HD6520G

  • SPA40 - slow

    Hi - I have an A40 SP 1.5 years, and for the last six months or so it was ridiculously slow. The software is constantly "not responding". I have the anit-virus and spyware AVG, doctor who show no virus or spyware that can slow down the system. I use

  • How to get rid pop-up windows "to connect to broadband?

    I login and always repeated pop window asking to connect very annoying please send advise "Thank you John

  • The trombone has disappeared when I receive an attachment!

    When you receive an e-mail, there is a paper clip in the corner where you can watch or record. Mine has disappeared and to view the attachment, I have to open the e-mail, then click with the right button on the attachment in the attachment line. It's

  • Reload MMC?

    My computer has had 2 unexpected stops.I started in Mode safe and said to recharge my MMC after a snapfresh or something was deleted.I can't find out where and how to charge MMC.Go to the root and enter the MMC console did not work ??