Conditional insert in plsql

Hi friends,
I am a beginner in Oracle PLSQL. I need to insert a row into a table based on a condition. The condition is to check if the value is already there in another table.
i.e, If the username is already present in the users table then the username should not be added to username table. I have written a stored procedure to insert into the username table but need i do not know how to check for the condition.
Could you please let me know how to get this. I tried a lot but getting compilation errors.
Regards,
Padeep

Use Control Statements
http://download.oracle.com/docs/cd/E11882_01/appdev.112/e17126/controlstatements.htm
Such as: IF-THEN-ELSE or CASE
That documentation is for 11.2. Your Oracle version may be different, but since IF statements have been around for a long time, this will do.
Alternatively, consider a unique index on the field.
http://download.oracle.com/docs/cd/E11882_01/server.112/e17120/indexes003.htm#ADMIN11724
Or even consider using a MERGE statement:
http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_9016.htm#SQLRF01606

Similar Messages

  • MERGE w/ conditional Insert clause

    Hi--
    Sum Up:Is it possible to run Merge syntax with update and a conditional Insert clauses ?
    Question Details
    Have a base table Emp and staging table New_Emp with a common identifier Emp_id.
    The ideal would be to merge the 2 tables and update Emp using New_Emp values and
    insert only some new emp, not all records.
    Any ideas ? Thks
    Lamine

    Absolutely. See the docs. Use the WHEN and WHERE clauses.
    Tom Best

  • Implementing Conditional Insert

    I use tips in the article "Implementing Conditional Insert, Update, and Delete Allows for View Objects" from "Steve Muench's Radio Weblog".
    At the start all it's ok, but I found this problem:
    1. I have two views (V1 and V2) over the same entity (E1)
    2. V1 permit insert, delete, update, and select.
    3. V2 overwrite method create and don't allow insert.
    4. If you use V1 and try to insert a record all go ok.
    5. If you use V2 and try to insert a record get an exception "It's not allow insert into V2", It's Ok.
    6. If you use V1 again and try to insert a record you get an exception, that say "It's not allow insert into V2".
    I'm investigate the case, and found that when you insert a record in E1 throw V1, the framework (BC4) try to create the record in the views that are instantiated over the entity E1, for this call method "create" on views.
    Do you know some workaround ?
    thank in advice...

    Hi, again, I have question about this...
    I post it in the forum...
    If I use "isAssociationConsistent()" I have to know all views that depend of same entity, and its privileges. I have problem with that because if I add a view a have to remember to put this in the code.
    I need to confirm: is return of method "this.getEntity(0).isInCache()" true when record was inserting in other entity ?
    May I modify your sample like this:
    public class CustomViewRowImpl extends ViewRowImpl {
    protected void create(AttributeList nvp) {
    if ( this.getEntity(0).isInCache() || getVO().isInsertAllowed()) {
    super.create(nvp);
    else {
    throw new JboException("Insert not allowed in this view");
    thank a lot

  • Conditional Insert within Merge???

    can we use conditional insert within the merge statement????
    thanks a lot..
    nantha

    NanthaKumarAR wrote:
    is there is any possible way to do so????
    thanks a alot ....
    nanthaHmmm let's look at the conversation so far...
    You: "is this possible?????"
    Me: "No."
    You: "is there is any possible way to do so????"
    I'm now wondering which part of "No" you failed to understand.
    You cannot perform multiple/conditional inserts within a MERGE statement. The purpose of a MERGE statement is to take data and MERGE it into a single table.
    The only messy way I can think of would be to merge into an intermediate table with triggers on it, that takes the merged data and splits it out into the seperate tables. Messy and not likely the best solution to the initial problem, which would probably be better solved by avoiding the use of MERGE in the first place (though we don't know what problem you are actually trying to solve).

  • Conditional Insert

    Hi,
    Is it possible to do a conditional insert? e.g.
    IF :P2_NAME != (SELECT name from hk.appname where name = :P2_NAME) then
    INSERT....
    END IF;So if the value of :P2_NAME doesn't exist within the table then it will be inserted...its to be an addition to a currently existing APEX process (checks that field values aren't null then enters a record into a different table than hk.appname).
    Thanks for any help.
    Mike

    Dird wrote:
    PLS-00405: subquery not allowed in this context As I said:
    IF :P6_NEW_PROJ IS NOT NULL THEN
    proj := lower(:P6_NEW_PROJ);
    ELSE
    proj := :P6_PROJECT;
    END IF;
    insert into hkadm.applic@XXX (appl_name) select proj from dual
    where 0 = (select count(*) from hkadm.applic@XXX where appl_name = proj and rownum = 1);SY.

  • Conditionally inserting tiles

    Hello All,
    I am using tiles with JSF. In my jsf page, i have the below logic but still the tiles get inserted...
    <% if(false){ %>
    <tiles:insert definition="NonMember" flush="false"/>
    <% } %>
    I am not sure whats happening.. does anyone know whats happening here...
    Basically,i want to check a condition and if the condition is false, i dont want to insert the tiles..

    Make sure nonMember definition is accessible via the jsp page:
    <f:verbatim>
    <%
    String nonMember = ...
    if(false)
    %>
    <tiles:insert name="<%=nonMember%>" flush="false" />
    <%
    %>
    </f:verbatim>
    Hope this may help.
    BaTien
    DBGROUPS

  • (no inline validation error displayed)) condition type and plsql expression

    Hello all
    Is it possible to use a plsql expression (eg: (:REQUEST = 'PXX_LIST_ITEM')) AND (no inline validation error displayed)) condition type at the smae time ?
    HTMLDB must be testing a substitution variable when the conditional display section is set to 'no inline validation errors displayed'.

    Hello,
    I don't know for sure because I never used it, but I think you can use the pl/sql variable htmldb_application.g_inline_validation_error_cnt. This variable identifies number of inline errors found.
    You can also check if the variable htmldb_application.g_notification contains a value.
    Fred.

  • Conditional Insertion in table based on Field

    Hi all,
      I am having 3 text input fields in my page, Empno, Ename, Sal
      After inserting data in the fields, commission column has to be populate automatically, based on conditions.
      Conditions are : If the salary is less than 5000 the commission 500 has to be insert, salary is greater than 5000 commission 1000 has to be inserted.
    how can I achieve this, and where I have to implement the logic, please help me out.
    Thanks in Advance
    Jagadish

    Hi,
    You can apply PPR on salary field. Then catch the event and based on condition, set the value of commission field.
    Also, you can check this link for PPR:
    http://sushantsharmaa.blogspot.in/2012/12/11-ppr-partial-page-rendering-in-oaf.html
    --Sushant

  • INSERT in PLSQL loop  in Oracle 9i  scheduled job has poor performance

    Hi,
    I have a scheduled job running in Oracle 9i. But everytime it executes the following piece of code, the INSERT process slows down drastically.
    -------------------------------typical piece of code having problem-----------------------------------
    LOOP
    FOR increbkgs IN bookings_cur (in_chr_fiscal_period_id,
    allrec.cust_name_string
    LOOP
    l_num_rec_count := l_num_rec_count + 1;
    INSERT INTO SA_PORTAL_CDW_BOOKINGS_INCTEMP
    (product_id, territory_code,
    global_target_id, service_type,
    equipment_deployment, created_date, updated_date,
    fiscal_period_id, customer_id,
    ship_to_country,
    bookings_amount, sams_alliance_id
    VALUES (increbkgs.product_id, increbkgs.territory_code,
    increbkgs.global_target_id, increbkgs.service_type,
    increbkgs.equipment_deployment, SYSDATE, SYSDATE,
    increbkgs.fiscal_period_id, increbkgs.customer_id,
    increbkgs.ship_to_country,
    increbkgs.bookings_amount, allrec.sams_alliance_id
    IF (l_num_rec_count = 500)
    THEN
    l_num_rec_count := 0;
    COMMIT;
    END IF;
    END LOOP;
    END LOOP;
    All the tablespaces are auto-extend. But we have still tried to increase the tablespace manually from 2% to 30% by adding datafiles. Still the INSERT is slowing down for some reason.
    (The same process in Oracle 8i is much faster)
    Any hint or guidance is greatly appreciated.
    Thanks and regards,
    Ambili

    commits in loops are great for slowing down things. Actually commits in loops are just about the best way of stalling any 'process' in Oracle.
    A much better way is to resize your undo tablespace to permit one single commit at the end of the whole thing. Yes it could be big, but that's the way Oracle works.
    If you want more info about this, buy Thomas Kyte's book found at http://apress.com/book/bookDisplay.html?bID=10008

  • Conditional Insert into staging table by using sqlloader

    Hi,
    In Oracle apps I'm submitting a concurrent program programmatically which will call sqlloader and insert into a staging a table.
    This table consists of 30 columns.Program has one input parameter.If parameter value = REQUIRED Then it should insert into first three columns of staging table.If it's APPROVED then it should insert into first 10 columns of the same table.
    Data file is pipe delimited file which may or may not have all possible values :For Required,I many not have all three column values

    >
    I think you understood the thinks wrongly. OP marked UTL_FILE as the correct answer, which is again a server side solution.
    >
    Perhaps you failed to notice that the answer now marked correct was posted AFTER mine. I'm not clairvoyant and OP did not state whether a server side or client side solution was appropriate.
    I stand by my comments. Using an external table is an alternative to sql loader but, as I said, simply using an external table instead of sql loader doesn't address OPs problem.
    >
    And IMO, external table will be faster than UTL_FILE
    >
    I'd be more concerned with why OP wants to write a procedure to parse flat files, deal with data conversion errors and perform table inserts when Oracle already provides that functionality using either external tables or sql loader.
    I would suggest loading the file into a new staging table that can hold all of the columns that might be contained in the flat file. Then the appropriate data can be moved to OPs staging table according to the required business rules.
    Sounds like we each think OP should reconsider the architecture.

  • Conditional insertion into a table

    Hello:
    I've a base table into which records are inserted from results returned by a query. The query always runs on the current date every hour. There are some preconditions before insertion:
    1) The backdated records will remain as it is;
    2) If query finds a new Number it will be inserted in the table;
    3) If query finds a same number but Count is more, it will also inserted in the table.
    TABLE1-
    Number|PDATE|CATEGORY|MARKED|COUNT
    1|29-APR-2012|C1|Y|2
    3|30-APR-2012|C1|Y|1
    4|30-APR-2012|C1|Y|1
    5|30-APR-2012|C1|Y|3
    Query Result-
    Number|TDATE|COUNT
    1|30-APR-2012|1
    2|30-APR-2012|5
    3|30-APR-2012|1
    4|30-APR-2012|9
    5|30-APR-2012|3
    Output: (Updated Table-1)
    Number|PDATE|CATEGORY|MARKED|COUNT
    1|29-APR-2012|C1|Y|2
    1|30-APR-2012|C1|N|1
    2|30-APR-2012|C1|N|5
    3|30-APR-2012|C1|Y|1
    4|30-APR-2012|C1|Y|1
    4|30-APR-2012|C1|N|9
    5|30-APR-2012|C1|Y|3
    Explain Output: The query is running on 30-Apr-2012
    1|29-APR-2012|C1|Y|2: This is a backdated record. So, remained unchanged.
    1|30-APR-2012|C1|N|1: 1 of today was not in the table. So, it is inserted with Status 'N'
    2|30-APR-2012|C1|N|5: 2 of today was not in the table. So, it is inserted with Status 'N'
    3|30-APR-2012|C1|Y|1: 3 is already in the table. So, unchanged.
    4|30-APR-2012|C1|N|9: 4 will be inserted cause count (9) is more than previous (1). So, it is inserted with Status 'N'
    5|30-APR-2012|C1|Y|3: 5 is already in the table. So, unchanged.
    Can anyone help me with query?
    Thanks/Razin

    Hope below helps you
    CREATE TABLE T
    (  NUM      NUMBER,
       PDATE    DATE,
       CATEGORY VARCHAR2(2),
       MARKED   VARCHAR2(1),
       COUNT    NUMBER
    INSERT INTO T VALUES (1,'29-APR-2012','C1','Y',2);
    INSERT INTO T VALUES (3,'30-APR-2012','C1','Y',1);
    INSERT INTO T VALUES (4,'30-APR-2012','C1','Y',1);
    INSERT INTO T VALUES (5,'30-APR-2012','C1','Y',3);
    INSERT INTO T(NUM, PDATE, CATEGORY, MARKED, COUNT )
    WITH QUERY_RESULT AS
    ( SELECT 1 NUM , TO_DATE('30-APR-2012','DD-MON-YYYY') PDATE,1 CNT FROM DUAL UNION ALL
      SELECT 2     , TO_DATE('30-APR-2012','DD-MON-YYYY'),5 FROM DUAL UNION ALL
      SELECT 3     , TO_DATE('30-APR-2012','DD-MON-YYYY'),1 FROM DUAL UNION ALL
      SELECT 4     , TO_DATE('30-APR-2012','DD-MON-YYYY'),9 FROM DUAL UNION ALL
      SELECT 5     , TO_DATE('30-APR-2012','DD-MON-YYYY'),3 FROM DUAL
    SELECT R.NUM, R.PDATE, 'C1','N', R.CNT
    FROM QUERY_RESULT R
    WHERE NOT EXISTS ( SELECT NULL
                       FROM T
                       WHERE R.NUM = T.NUM AND ( R.PDATE < T.PDATE OR (R.PDATE = T.PDATE AND R.CNT <= T.COUNT ))
    SELECT * FROM T
           NUM PDATE     CATEGORY   MARKED          COUNT
             1 29-APR-12 C1         Y                   2
             1 30-APR-12 C1         N                   1
             2 30-APR-12 C1         N                   5
             3 30-APR-12 C1         Y                   1
             4 30-APR-12 C1         Y                   1
             4 30-APR-12 C1         N                   9
             5 30-APR-12 C1         Y                   3
    {code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Implementing Conditional Insert, Update, and Delete Allows for View Objects

    Steve,
    Thanks for the info in this article it was very helpful. I have one question as I created a test case that used dept & emp tables as master and detail.
    Placing code in the emp detail ViewRowImpl that also tests to see if the master is updateable and it all works fine but I noticed that when you run it in the tester the dept master fields are grayed out but not the emp details and if you bring up the emp detail with out the view link then they are grayed out.
    Just wondering is this just a fact of the detail being shown in a table in the master detail and not a form or is there more to it then that??

    yes as follows:
    isMasterUpdateable checks the Dept table to see if it is updateable before allowing emp to be updated.
    public boolean isAttributeUpdateable(int index)
    if ( isMasterUpdateable() == true )
    return ( super.isAttributeUpdateable(index) );
    return(false);
    When I run this in the tester the results are correct except for the visual where the form is grayed out but the detail in the table is not. Should the table gray out also or is this effect something that must be coded in all Jtables that i may implement? If it needs to be coded to get the effect where does Jtable check for isAttributeUpdateable ?
    thanks,

  • How to insert some records in one table and some records in another table

    Interview question
    how insert records in two tables by using trigger
    CREATE or REPLACE TRIGGER Emp_Ins_Upd_Del_Trig
    BEFORE delete or insert or update on EMP
    FOR EACH ROW
    BEGIN
    if UPDATING then
    UPDATE emp2
    SET
    empno = :new.empno,
    ename = :new.ename
    --, job = :new.job
    --, mgr = :new.mgr
    --, hiredate = :new.hiredate
    , sal = :new.sal
    --, comm = :new.comm
    --, deptno = :new.deptno;
    sdate = :new.sdate,
    edate = :new.edate
    end if;
    if INSERTING then
    INSERT INTO emp2
    VALUES
    ( :new.empno
    , :new.ename
    --, :new.job
    --, :new.mgr
    --, :new.hiredate
    , :new.sal
    --, :new.comm
    --, :new.deptno
    new.sdate,
    new.edate);
    end if;
    if DELETING then
    DELETE FROM emp2
    WHERE empno = emp2.empno;
    end if;
    END;
    it is working fine but he wants to insert some specific litimit on one table and some specified limit of records in one ..
    In this senerio can i insert records by use count of records...
    please help me..

    Can you be more specific on the "Limit"
    Conditional insert can be used in this case.

  • Use of insert in case

    Hi,
    Is it possible to use insert statement in CASE.
    For example as follows:
    let create a table as
    create table DECOMPENSATED_BALANCE_TMP (ENDING_ENTERED_BALANCE_DR number, ENDING_ENTERED_BALANCE_CR number);
    select
    CASE
    WHEN (PERIOD_NET_DR+BEGIN_BALANCE_DR-PERIOD_NET_CR-BEGIN_BALANCE_CR) total > 0
    THEN
    INSERT INTO XXGL_DECOMPENSATED_BALANCE_TMP (ENDING_ENTERED_BALANCE_DR) values (total)
    WHEN (PERIOD_NET_DR+BEGIN_BALANCE_DR-PERIOD_NET_CR-BEGIN_BALANCE_CR) <= 0
    THEN
    INSERT INTO XXGL_DECOMPENSATED_BALANCE_TMP (ENDING_ENTERED_BALANCE_CR) values (total)
    else
    NULL
    end
    from gl_balances
    where code_combination_id=109940 and currency_code='GBP';
    Any answers will be highly appreciated ....
    Thanks,
    zaheer

    I don't think that a conditional insert is really needed in this case. THe only part where the insert differse is the else part of your case statment where no insert should be done. THis can be made into a where clause.
    An easy way would be to use a simple INSERT + SELECT statement. Where the value for the dr and cr columns are created by a case or a decode statement.
    untested example
    insert into XXGL_DECOMPENSATED_BALANCE_TMP
      (ENDING_ENTERED_BALANCE_DR, ENDING_ENTERED_BALANCE_CR)
    select case WHEN (PERIOD_NET_DR+BEGIN_BALANCE_DR-PERIOD_NET_CR-BEGIN_BALANCE_CR) > 0  then total
              else null
              end as DR
             ,case WHEN (PERIOD_NET_DR+BEGIN_BALANCE_DR-PERIOD_NET_CR-BEGIN_BALANCE_CR) <= 0  then total
              else null
              end as CR
    from gl_balances
    where code_combination_id=109940
    and currency_code='GBP'
    and (PERIOD_NET_DR+BEGIN_BALANCE_DR-PERIOD_NET_CR-BEGIN_BALANCE_CR) is not null -- else part
    ;The good thing is you could run this insert statement for several code_combination_ids directly in SQL without any pL-loop.
    Just noticed that leonard aready posted almost the same solution.
    Edited by: Sven W. on Aug 24, 2011 3:22 PM

  • Maintain Excise defaults for pricing condition in TAXINN procedure

    Hi all,
    I post this message as I'm customizing CIN part for creation and calculation of excise invoice. I have read lot of post but i'm a bit confused about right procedure to adopt. I try to explain:
    I'm doing a stardard sales flow (sales order -> delivery -> commercial invoice -> excise invoice) and the only method I have found to flow excise values in excise invoice is to insert pricing conditions in India -> Basic settings -> Determination of excise duty -> Maintain excise defaults for TAXINN procedure.
    In this table I have insert pricing conditions I use in my SD pricing schemas (in this case I use a copy of JINFAC). I have insert pricing conditions corresponding to BED % and BED total, and also CESS % and total and S&HCESS % and total.
    If I remove pricing condition from this table, corresponing value in excise invoice, when I create it, become 0.
    My dubt is if is correct to populate with table to flow excise values in excise invoice. I ask it as I have read differents approach and differents customizing settings applied in similar situations. In particular, I have found documentation in which, for TAXINN procedure, is recommended to leave blank "excise defaults" (with exception of CVD condition). What is the right method to use?
    There is one right procedure or, as it seems reading posts, someone have to do his "proper CIN customizing" based on results that have to obtain?
    Thanks in advance for any contribute that can help to clarify this situation.
    Regards
    Gianpaolo

    Hi all,
    I think to have found solution to my dubt.
    If I use "condition based Excise determination" I have to insert records with pricing schema (if is a copy of standard J* schema, like ZINFAC that is a copy of JINFAC) and excise pricing conditions in the transaction below:
    India -> Basic settings -> Determination of excise duty -> Condition-based Excise determination -> Classify condition type
    To determine value in excise invoice seems that program check before table in transaction described before. If in this table don't find records for pricing schema-excise pricing condition the program check also table in transaction below:
    India -> Basic settings -> Determination of excise duty -> Maintain excise defaults
    If during this second check find excise pricing conditions insert, can determine equally the values of excise in excise invoice.
    This is the result of my checks. I hope this could be useful for other people with similar problem.

Maybe you are looking for

  • Naming trace file

    I have generated many trace files for analysis using tkprof. However, I have a difficulty in locating the exact trace file (.trc) from 100s of trace files. Naming my trace file will save time. Is there a way to name the trace file when I start tracin

  • Moving to Office365 but keeping IronPort Appliances

    We are moving our internal e-mail system to Office365 and I'm starting to plan the mail routing.  Basically all inbound and outbound e-mail will still go through our IronPort appliances.   Outbound e-mail doesn't look too bad, but I'm wondering about

  • Cover Art Corruption?

    Hello. I bought an iPod Touch 32 GB (3rd Gen.), model A318/EMC2310 about 1 month ago. It came with the 3.1.2 firmware, and I updated it to 3.1.3. I noticed some days ago that something pretty "wrong" and annoying started happening: The cover art for

  • RDNSS Support in IOS

    According to Wikipedia, IOS supports RDNSS or the abillity to configure IPv6 DNS Server(s) via RA (RFC 6106): http://en.wikipedia.org/wiki/Comparison_of_IPv6_support_in_operating_systems However, I can't figure out how to set this up.  Can someone pr

  • Could not synchronize

    Hi there, I'm testing a small app while trying to learn Java Persistence . Just a newbie at this... In my first unit of work I create and persist a ScheduledCourse object like so: EntityManagerFactory emf = Persistence.createEntityManagerFactory("sch