Case in Update statement

hi,
How to use the case or decode statement in Update statement.

Welcome to Oracle Technology network!
This forum is for SQL*Developer issues - things realed directly to Oracle's SQL*Developer product. Your quesiton about CASE statements in UPDATES is a general SQL question. You will get a better answer if you close this thread and open it again in the SQL and PL/SQL forum which is for SQL questions

Similar Messages

  • Update statement using case

    Hello my friends
    I have one table "encompasses": continent,country,percentage
    now the update should change the continent field, all "Europe" to "Asia" and all "Asia" to "America" and "America" to "Europe" by using case and just one update statement.
    I wrote so and i got this error
    Error starting at line 2 in command:
    UPDATE encompasses
       SET continent=    
          CASE  WHEN continent='Europe'  THEN 'Asia'  
          WHEN continent='Asia'  THEN 'America' 
          WHEN continent='America' THEN 'Europe'
          END
    Error report:
    SQL Error: ORA-01407: cannot update ("intern"."ENCOMPASSES"."CONTINENT") to NULL
    01407. 00000 -  "cannot update (%s) to NULL"
    *Cause:   
    *Action:please give me hints,thank u.
    --  File created - Monday-May-13-2013  
      CREATE TABLE "intern"."ENCOMPASSES" ("COUNTRY" CHAR(2), "CONTINENT" VARCHAR2(20), "PERCENTAGE" NUMBER)
       COMMENT ON COLUMN "intern"."ENCOMPASSES"."COUNTRY" IS 'the country code'
       COMMENT ON COLUMN "intern"."ENCOMPASSES"."CONTINENT" IS 'the continent name'
       COMMENT ON COLUMN "intern"."ENCOMPASSES"."PERCENTAGE" IS 'percentage, how much of the area of a country belongs to the
                continent'
       COMMENT ON TABLE "intern"."ENCOMPASSES"  IS 'information to which continents a country belongs'
    REM INSERTING into intern.ENCOMPASSES
    SET DEFINE OFF;
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('cx','Asia',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('eg','Asia',10);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('ge','Asia',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('id','Asia',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('ad','Europe',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('al','Europe',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('at','Europe',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('ba','Europe',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('pa','America',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('pe','America',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('pm','America',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('pr','America',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('py','America',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('sr','America',100);
    Insert into intern.ENCOMPASSES (COUNTRY,CONTINENT,PERCENTAGE) values ('sv','America',100);thank you, im using oracle 11g and ubuntu12
    best,david
    Edited by: 1003209 on 13-May-2013 10:12

    1003209 wrote:
    Hello my friends
    I have one table "encompasses": continent,country,percentage
    now the update should change the continent field, all "Europe" to "Asia" and all "Asia" to "America" and "America" to "Europe" by using case and just one update statement.
    I wrote so and i got this error
    Error starting at line 2 in command:
    UPDATE encompasses
        SET continent=    
           CASE  WHEN continent='Europe'  THEN 'Asia'  
           WHEN continent='Asia'  THEN 'America' 
           WHEN continent='America' THEN 'Europe'
           END
    Error report:
    SQL Error: ORA-01407: cannot update ("intern"."ENCOMPASSES"."CONTINENT") to NULL
    01407. 00000 -  "cannot update (%s) to NULL"
    *Cause:   
    *Action:This seems the column Continent has not null contraint on it, hence when there is a continent other than the three you mentioned above the case statement is evaluating it to null and hence failing e.g. if there is a record with continent = 'Antartica' it will try to update that as null.
    so either add this:
    WHERE continent in ('Europe' ,'Asia','America')this will update only for this continents and ignore other values.
    If you want to update other continents as well then have a else condition in CASE statement to assign defualt value.
    Regards,
    Santosh

  • Update statement with Case syntax

    I want to put case statement in an update statement using Oracle 10g
    I'm getting a syntax error on the last line Ora-00933: SQL command not properly ended
    Help please
    UPDATE EMP a
    SET EMP.TYPE = CASE
    WHEN 'Y' = 'A' THEN 'DIV'
    WHEN 'Y' = '1' THEN 'DEF'
    END CASE;

    Hi,
    Samim wrote:
    I want to put case statement in an update statement using Oracle 10g
    I'm getting a syntax error on the last line Ora-00933: SQL command not properly ended
    Help please
    UPDATE EMP a
    SET EMP.TYPE = CASE
    WHEN 'Y' = 'A' THEN 'DIV'
    WHEN 'Y' = '1' THEN 'DEF'
    END CASE;The closing keyword that corresponds to "CASE" is "END", not "END *CASE* ".
    The string 'Y' is never equal to 'A', or to '1'.
    If you have a column named y in the table, then you might want to say:
    UPDATE EMP
    SET EMP.TYPE = CASE
                        WHEN Y = 'A' THEN 'DIV'
                        WHEN Y = '1' THEN 'DEF'
                  END;I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statemnts to re-create your emp table as it exists before the UPDATE), and the results you want from that data (that is, the contents of the emp table after the UPDATE).

  • Update statement that includes case, decode and from clause

    hello
    I've the following code. I ran this code in 9i and got the errror SQL command not properly ended. Can I use FROM clause in an update statement?
    CREATE OR REPLACE procedure NRI
    IS
    BEGIN
    UPDATE IEB2 SET SDI =
    (CASE PSDI WHEN '11' THEN '16'
    WHEN '13' THEN '38'
    WHEN '14' THEN '18'
    WHEN '23' THEN '21'
    WHEN '24' THEN '21'
    WHEN '31' THEN '27'
    WHEN '32' THEN '37'
    WHEN '33' THEN '38'
    WHEN '34' THEN '37'
    WHEN '43' THEN '46'
    WHEN '53' THEN '45'
    WHEN '55' THEN '48'
    WHEN '60' THEN '54'
    WHEN '61' THEN '57'
    WHEN '62' THEN '54'
    WHEN '63' THEN '56'
    WHEN '64' THEN '52'
    WHEN '70' THEN '21'
    WHEN '77' THEN '25'
    WHEN '78' THEN '41'
    WHEN '80' THEN '79'
    WHEN '85' THEN '75'
    WHEN '87' THEN '73'
    WHEN '15' THEN '15'
    WHEN '51' THEN '51'
    WHEN '88' THEN '88'
    WHEN '00' THEN '00'
    WHEN '99' THEN '99'
    WHEN '12' THEN DECODE(C.R_ID, 'N', '16','18')
    ELSE 'NONE' END)
    FROM D1.C C
    WHERE EXISTS
    (SELECT 1
    FROM D1.SD A, D2.SDP B, D1.C C
    WHERE A.SDN = B.DN
    AND B.SDI = '000000'
    AND B.PFI = 'W'
    AND B.CID = C.CID
    AND A.SDN = '0001500721');
    END;

    Your parenthesis look a tad messed up ... think this fixes them.
    CREATE OR REPLACE procedure NRI
    IS
    BEGIN
    UPDATE IEB2 SET SDI =
    SELECT
    CASE PSDI
      WHEN '11' THEN '16'
      WHEN '13' THEN '38'
      WHEN '14' THEN '18'
      WHEN '23' THEN '21'
      WHEN '24' THEN '21'
      WHEN '31' THEN '27'
      WHEN '32' THEN '37'
      WHEN '33' THEN '38'
      WHEN '34' THEN '37'
      WHEN '43' THEN '46'
      WHEN '53' THEN '45'
      WHEN '55' THEN '48'
      WHEN '60' THEN '54'
      WHEN '61' THEN '57'
      WHEN '62' THEN '54'
      WHEN '63' THEN '56'
      WHEN '64' THEN '52'
      WHEN '70' THEN '21'
      WHEN '77' THEN '25'
      WHEN '78' THEN '41'
      WHEN '80' THEN '79'
      WHEN '85' THEN '75'
      WHEN '87' THEN '73'
      WHEN '15' THEN '15'
      WHEN '51' THEN '51'
      WHEN '88' THEN '88'
      WHEN '00' THEN '00'
      WHEN '99' THEN '99'
      WHEN '12' THEN DECODE(C.R_ID, 'N', '16','18')
      ELSE 'NONE'
    END
    FROM D1.C C
    WHERE EXISTS 
        SELECT 1
        FROM D1.SD A, D2.SDP B, D1.C C
        WHERE A.SDN = B.DN
        AND B.SDI = '000000'
        AND B.PFI = 'W'
        AND B.CID = C.CID
        AND A.SDN = '0001500721'
    END;

  • Update statement with Case

    Hi,
    I need to construct an SQL Update statement that uses a case statement.
    I was able to construct one:
    UPDATE tableB
    SET col1 = CASE WHEN (0, 0) = (SELECT col1, col2
    FROM tableA
    WHERE <tableA constraints> )
    THEN "this value"
    ELSE "that value"
    END
    WHERE <tableB constraints>;
    But what I need to do now is to have some comparison inside the CASE WHEN statement, like:
    UPDATE tableB
    SET col1 = CASE WHEN (SELECT col1 from tableA WHERE <tableA constraints> > 0
    AND
    SELECT col2 from tableA WHERE <tableA constraints> > 0
    THEN "this value"
    ELSE "that value"
    END
    WHERE <tableB constraints>;
    Basically, the case would need to pass two (or more) constraints before updating the valuefor tableB.
    By the way, I was able to do this with just one constraint inside the case statement, but I need to pass more than one constraint inside the case.
    Your inputs would be highly appreciated. Thanks.
    Regards,
    Edited by: BatutaBunsing on Jul 22, 2009 6:42 PM

    Example Test
    UPDATE emp
    SET    ename = CASE
                     WHEN (SELECT 1
                           FROM   dual
                           WHERE  1 = 1) > 0 --// condition TRUE
                          AND (SELECT 1
                               FROM   dual
                               WHERE  1 = 1) > 0  --// condition TRUE
                     THEN 'this value'
                     ELSE 'that value'
                   END
    WHERE  empno = 7369
    1 row updated.
    SQL> select * from emp ;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7369 this value CLERK           7902 17-DEC-80        800                    99
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
    UPDATE emp
    SET    ename = CASE
                      WHEN (SELECT 1
                            FROM   dual
                            WHERE  1 = 2) > 0 --// condition FALSE
                           AND (SELECT 1
                                FROM   dual
                                WHERE  1 = 1) > 0 --// condition TRUE
                      THEN 'this value'
                      ELSE 'that value'
                    END
    WHERE  empno = 7369
    1 row updated.
    SQL> select * from emp ;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7369 that value CLERK           7902 17-DEC-80        800                    99
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
    ...SS

  • Update statement with CASE and banding

    Hello Folks,
    I am stuck and am going nowhere.
    I have two tables
    MinuteCategory
    Minute
    MinuteCategory
    MinuteLookup
    MinuteCategoryId (surrogate key)
    MinuteLow
    MinuteHigh
    MinuteCategory
    Sample from Minute Lookup table ..
    1 1
    15 <15 min
    2 16
    30 <30 min
    3 31
    45 <45 min
    4 46
    60 <60 minutes
    5 61
    75 <1 hour 15 minutes
    I am trying to update the MinuteCategory column in the MinuteCategory table using the MinuteLookup table.
    For example if the minute = 33 then the corresponding lookup value would be 3 (from the lookup table), because the 
    33rd minute falls in between 31 min and 45 min and the corresponding surrogate key is 3
    Is this possible with an Update statement in SQL? The only thing i can think of is use case statement as the join key between
    the two tables but i don't think it's going to work :(
    UPDATE
    SET MinuteCategory = ml.MinuteCategoryId
    FROM MinuteCategory mc join MinuteLookup ml on mc.Minute = 
    CASE WHEN mc.Minute between ml.MinuteLow and ml.MinuteHigh THEN ml.MinuteCategoryId ELSE NULL
    END
    Would appreciate any help.
    SS

    Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
    use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. 
    This is minimal polite behavior on SQL forums. 
    >> I have two tables <<
    Have you read any book on data, so you have some idea what a “<something>_category” means? THINK!! A minute is unit of temporal measurement. By definition, it cannot be a category. 
    There is no such thing as a “<something>_category_id”; a data element is a “<something>_category” or a “<something>_id”, as per basic data modeling. 
    Use a table of time slots set to one more decimal second of precision than your data. You can now use temporal math to add it to a DATE to TIME(1) get a full DATETIME2(0). Here is the basic skeleton. 
    CREATE TABLE Timeslots
    (slot_start_time TIME(1) NOT NULL PRIMARY KEY,
     slot_end_time TIME(1) NOT NULL,
     CHECK (start_time < end_time));
    INSERT INTO Timeslots  --15 min intervals
    VALUES ('00:00:00.0', '00:14:59.9'),
    ('00:15:00.0', '00:29:59.9'),
    ('00:30:00.0', '00:44:59.9'),
    ('00:45:00.0', '01:00:59.9'), 
    ('23:45:00.0', '23:59:59.9'); 
    Here is the basic query for rounding down to a time slot. 
    SELECT CAST (@in_timestamp AS DATE), T.start_time
      FROM Timeslots AS T
     WHERE CAST (@in_timestamp AS TIME)
           BETWEEN T.slot_start_time 
               AND T.slot_end_time;
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • Unable to execute an update statement using CallableStatement

    Hi there,
    I'm trying to run an update statement from JUnit using java.sql.CallableStatement and oracle.jbo.server.DBTransaction.
            String updateSql =
                "update footable set barcol=TO_DATE('12-SEP-09','dd-MM-yy') where bazcol = 505";
            try {
                statement =
                        applnModule.getDBTransaction().createCallableStatement(updateSql,
                                                                               2);
                int executeUpdate = statement.executeUpdate();
                AppsLogger.write(this,
                                 "# records UPDATED ------------------>" + executeUpdate,
                                 AppsLogger.SEVERE);
            } catch (SQLException s) {
                s.printStackTrace();
                Assert.fail("Encountered SQL Exception: " + s);
            } finally {
                try {
                    if (statement != null)
                        statement.close();
                } catch (SQLException s) {
            }Below is the exception I get when I run the above code. There is no problem with the SQL - it works fine from SQLDeveloper.
    java.lang.AssertionError: Encountered SQL Exception: java.sql.SQLDataException: ORA-01858: a non-numeric character was found where a numeric was expected
         org.junit.Assert.fail(Assert.java:91)
         sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         java.lang.reflect.Method.invoke(Method.java:597)
         org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
         org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:105)
         org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
         org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
         org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
         org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
         oracle.apps.common.applicationsTestFramework.junit.internal.AtfJUnit4JTestCaseClassRunner.invokeTestMethod(AtfJUnit4JTestCaseClassRunner.java:362)
         oracle.apps.common.applicationsTestFramework.junit.internal.AtfJUnit4JTestCaseClassRunner.runMethods(AtfJUnit4JTestCaseClassRunner.java:272)
         oracle.apps.common.applicationsTestFramework.junit.internal.AtfJUnit4JTestCaseClassRunner$1.run(AtfJUnit4JTestCaseClassRunner.java:265)
         org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
         org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
         oracle.apps.common.applicationsTestFramework.junit.internal.AtfJUnit4JTestCaseClassRunner.run(AtfJUnit4JTestCaseClassRunner.java:262)Edited by: 911023 on Oct 2, 2012 11:28 AM
    Edited by: 911023 on Oct 2, 2012 11:30 AM

    Using case statement.
    UPDATE gor_gold_post
       SET hoov_flag = CASE WHEN TRUNC (ADD_MONTHS (rec.loyalty_date, rec.loyalty_period) - SYSDATE) < 304
                                   OR
                                   (TRUNC (ADD_MONTHS (rec.loyalty_date, rec.loyalty_period) - SYSDATE) IS NULL
                                AND (SYSDATE - TO_DATE (rec.contract_date, 'YYYYMMDD')) > 91.2)
                           THEN 1
                           ELSE 99
                         END,
           b49n      = CASE WHEN TRUNC (ADD_MONTHS (rec.loyalty_date, rec.loyalty_period) - SYSDATE) < 121.6
                             OR
                             (TRUNC (ADD_MONTHS (rec.loyalty_date, rec.loyalty_period) - SYSDATE) IS NULL
                                AND (SYSDATE - TO_DATE (rec.contract_date, 'YYYYMMDD')) > 91.2)
                           THEN 1
                           ELSE 99
                         END
    WHERE tariff_code IN (169, 135, 136);Note: Code not tested.

  • How do I pass multiple values from a text box to an update statement

    I hope this does not sound to lame. I am trying to update multiple values Like this:
    Code|| Computer Desc || Computer Price || Computer Name
    SEL1 || Apple macbook || 1564 || Apple Macbook Basic
    SEL2 || Dell 630 || 1470 || Dell Latitude
    I want to change all six values at once in one update statement based on the Code, I can't find a good tutorial/example to help me.
    Can anyone point me in the right direction?
    Thanks so much,
    Laura

    You can do conditional updates with decode or case statements e.g.
    SQL> create table t as
      2  select 'SEL1' as code, 'Apple macbook' as comp_desc, 1564 as comp_price, 'Apple Maxbook Basic' as comp_name from dual union
      3  select 'SEL2', 'Dell 630', 1470, 'Dell Latitude' from dual
      4  /
    Table created.
    SQL>
    SQL> update t
      2  set comp_desc = CASE code WHEN 'SEL1' THEN 'Test1' Else 'Test2' END,
      3      comp_price = CASE code WHEN 'SEL1' THEN 1234 Else 2345 END,
      4      comp_name = CASE code WHEN 'SEL1' THEN 'Test1 Name' Else 'Test2 Name' END
      5  /
    2 rows updated.
    SQL>
    SQL> select * from t
      2  /
    CODE COMP_DESC     COMP_PRICE COMP_NAME
    SEL1 Test1               1234 Test1 Name
    SEL2 Test2               2345 Test2 Name
    SQL>

  • Update statement getting ORA-00904 invalid identifier

    I am getting this ORA error when I try to conduct an update statement from a column value in one table to a like-named column in another table.
    For instance, in my select, shown below, this query runs like a champ, returning all of my records
    SELECT
    b.ID,
    b.ZIP, a.ZIP,
    a.NAME,
    a.LOCATION
    FROM TBL_ARCHIVE a
    INNER JOIN TBL_UAT b ON
    a.ID = b.ID
    WHERE
    b.ID > 470000  And b.ID <470100;However, if I try to run an update statement with any of the following, I get this error. It seems so strange to me that I'd get an error using the same columns in the update statement, but would return the desired results effectively from the select statement!
    one version of UPDATE
    UPDATE TBL_ARCHIVE a
    SET a.ZIP = TBL_UAT.ZIP
    WHERE TBL_UAT.ID = a.ID;and another
    UPDATE TBL_ARCHIVE
    SET a.ZIP  =
    (SELECT b.ZIP
    FROM TBL_UAT b
    WHERE b.ID <472500 And b.ID >471000)
    FROM TBL_ARCHIVE a
    WHERE a.ID = b.ID)
    WHERE ID IN
    (SELECT ID
    FROM TBL_UAT b
    WHERE b.ID  <472500 And b.ID >471000
    );^ - this one produces a SQL not properly ended. Error ORA-00933: SQL command not properly ended.
    I'm really unsure though, how what I thought was a fairly basic update statement wouldn't work. I'm using Oracle 11g but through either Toad or SQL Plus. I've gotten these same errors in both.
    Though MS Access runs slow as snails, I can manage to run the update statement successfully with it, a thousand records or so at a time. Problem is, I've got about 300K records needing updating.
    I've looked around for similar problems but haven't found one where someone wasn't either using a reserved word for a column, which I'm not, I don't believe, or a problem that really dealt with this error in the context of an update statement.
    But I'd welcome any enlightenment on this.

    rp0428 wrote:
    UPDATE TBL_ARCHIVE a
    SET a.ZIP = TBL_UAT.ZIP
    WHERE TBL_UAT.ID = a.ID;That isn't a valid query and neither is the other one.
    Please post the actual query you are using and the actual complete error message you are getting (everything about the error that is displayed).^^
    That is NOT a valid query?
    How else would you have an UPDATE statement created to set the value of a column in table A to the value of a (in this case, same named) column in table B?
    And keep them in order to update the records appropriately?
    I'll append the create statements momentarily.
    Thanks!
    CREATE TABLE TBL_UAT
    ( ID NUMBER(6),
    ZIP VARCHAR2(10 BYTE) ) and
    CREATE TABLE TBL_ARCHIVE
    ( ID NUMBER(6) NOT NULL, NAME VARCHAR2(50 BYTE),
    EMAIL VARCHAR2(50 BYTE),
    LOCATION VARCHAR2(50 BYTE),
    DEPARTMENT VARCHAR2(50 BYTE),
    PRIORITY VARCHAR2(50 BYTE),
    APPROVING_MGR_NAME VARCHAR2(50 BYTE),
    TYPE1 VARCHAR2(50 BYTE),
    TYPE2 VARCHAR2(50 BYTE),
    CONTACT_NAME VARCHAR2(50 BYTE),
    CONTACT_PHN_NO VARCHAR2(50 BYTE),
    LOAN_NUM VARCHAR2(50 BYTE),
    REF_ID VARCHAR2(50 BYTE),
    BORROWER_NAME VARCHAR2(50 BYTE),
    ADDRESS VARCHAR2(50 BYTE),
    CITY VARCHAR2(50 BYTE),
    ST VARCHAR2(50 BYTE),
    ZIP VARCHAR2(10 BYTE),
    SALE_DATE DATE,
    COMMENTS VARCHAR2(800 BYTE),
    TIME_REQ DATE,
    ACCTLOC VARCHAR2(50 BYTE),
    OTHER_ACCTLOC VARCHAR2(50 BYTE)) Hope this can be of assistance.
    Thanks, folks!
    Edited by: user515689 on Feb 7, 2012 11:59 AM

  • Write an UPdate statement using the logic used in PL/SQL block (oracle 10g)

    Hi All,
    I have written the following PL/SQL block. I want to write an UPDATE statement using the logic used in the following PL/SQL block. can any one please help me out in this regards.
    DECLARE
       v_hoov_fag   gor_gold_post.hoov_flg%TYPE;
       v_b49n          gor_gold_post.b49n%TYPE;
       CURSOR c
       IS
          SELECT bs_id, loyalty_date, loyalty_period, contract_date
            FROM gor_gold_post
           WHERE tariff_code IN (169, 135, 136);
    BEGIN
       FOR rec IN c
       LOOP
          IF    (TRUNC (ADD_MONTHS (rec.loyalty_date, rec.loyalty_period)
                        - SYSDATE) < 304
             OR (    TRUNC (  ADD_MONTHS (rec.loyalty_date, rec.loyalty_period)
                            - SYSDATE
                           ) IS NULL
                 AND (SYSDATE - TO_DATE (rec.contract_date, 'YYYYMMDD')) > 91.2
          THEN
             v_hoov_flg := 1;
          ELSE
             v_hoover_flag := 99;
          END IF;
          IF    (TRUNC (ADD_MONTHS (rec.loyalty_date, rec.loyalty_period)
                        - SYSDATE) < 121.6
             OR (    TRUNC (  ADD_MONTHS (rec.loyalty_date, rec.loyalty_period)
                            - SYSDATE
                           ) IS NULL
                 AND (SYSDATE - TO_DATE (rec.contract_date, 'YYYYMMDD')) > 91.2
          THEN
             v_b49n := 1;
          ELSE
             v_b49n := 99;
          END IF;
          UPDATE gor_gold_post
             SET hoov_flg = v_hoov_flg,
                 b49n = v_b49n
           WHERE bs_id = rec.bs_id AND tariff_code IN (169, 135, 136);
          COMMIT;
       END LOOP;
    END;Thank you,

    Using case statement.
    UPDATE gor_gold_post
       SET hoov_flag = CASE WHEN TRUNC (ADD_MONTHS (rec.loyalty_date, rec.loyalty_period) - SYSDATE) < 304
                                   OR
                                   (TRUNC (ADD_MONTHS (rec.loyalty_date, rec.loyalty_period) - SYSDATE) IS NULL
                                AND (SYSDATE - TO_DATE (rec.contract_date, 'YYYYMMDD')) > 91.2)
                           THEN 1
                           ELSE 99
                         END,
           b49n      = CASE WHEN TRUNC (ADD_MONTHS (rec.loyalty_date, rec.loyalty_period) - SYSDATE) < 121.6
                             OR
                             (TRUNC (ADD_MONTHS (rec.loyalty_date, rec.loyalty_period) - SYSDATE) IS NULL
                                AND (SYSDATE - TO_DATE (rec.contract_date, 'YYYYMMDD')) > 91.2)
                           THEN 1
                           ELSE 99
                         END
    WHERE tariff_code IN (169, 135, 136);Note: Code not tested.

  • ORA-01403: No data found (Occured in UPDATE Statement)

    Dear All,
    I am getting No data found error while executing the following UPDATE statement.
    UPDATE DEAL
    SET CLIENT_CD = 'HDMJARVN'
    WHERE CA_REF_NO = 70728
    AND CLIENT_CD = 'HDMJARVI';
    Wheareas
    SELECT *
    FROM DEAL
    WHERE CA_REF_NO = 70728
    AND CLIENT_CD = 'HDMJARVI';
    Gives me 1 row.
    Please explain.....
    Thanks in Advance.

    UPDATE DEAL
    t line 1:
    ORA-01403: no data foundHi Yogesh,
    I created a trigger on a table "CHILD" which fires during UPDATE statement. As you may notice, the trigger raises "no data found" exception which is not handled as a result the update ends up with an error.
    Similar is your case, investigate the trigger to resolve the issue.
    SQL> desc child
    Name                                      Null?    Type
    B                                                  NUMBER
    SQL> create or replace trigger child_trig
      2  after update of b on child
      3  referencing old as old new as new
      4  for each row
      5  declare
      6    l_dummy number;
      7  begin
      8    select 1 into l_dummy from dual where dummy = 'TEST';
      9  end;
    10  /
    Trigger created.
    SQL> select * from child;
             B
             1
    SQL> update child set b = 5;
    update child set b = 5
    ERROR at line 1:
    ORA-01403: no data found
    ORA-06512: at "TEST.CHILD_TRIG", line 4
    ORA-04088: error during execution of trigger 'TEST.CHILD_TRIG'
    SQL>Regards

  • Performance degradation in Update statement

    Hi
    I have a table t1 with 3 of the columns as :- c1 ,c2,c3.
    there is a non unique index on c2 ,c3.
    I am using an update statement as : update t1 set c1="value" where c2="some_value" and c3="some_value2" ;
    this works fine in case the number of rows for the same c2 and c3 are less (~100) but takes a large time when ~30,000.
    And it has to be run many times in my application (time out is occuring now)
    Can anybody suggest a solution ?
    Thanks
    Message was edited by:
    user580975

    First off, setting a table to NOLOGGING does not affect the rate of redo generation unless you are also doing direct-path inserts (which is obviously not the case here and which come with their own set of issues). Note that the amount of redo being generated in these two examples is quite similar.
    SCOTT @ jcave102 Local> create table all_obj_cpy as select * from all_objects;
    Table created.
    Elapsed: 00:00:10.78
    SCOTT @ jcave102 Local> set autotrace traceonly;
    SCOTT @ jcave102 Local> alter table all_obj_cpy logging;
    Table altered.
    Elapsed: 00:00:00.01
    SCOTT @ jcave102 Local> update all_obj_cpy
      2  set last_ddl_time = sysdate - 1;
    42511 rows updated.
    Elapsed: 00:00:01.45
    Execution Plan
    Plan hash value: 1645016300
    | Id  | Operation          | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | UPDATE STATEMENT   |             | 43776 |   384K|   137   (3)| 00:00:02 |
    |   1 |  UPDATE            | ALL_OBJ_CPY |       |       |            |          |
    |   2 |   TABLE ACCESS FULL| ALL_OBJ_CPY | 43776 |   384K|   137   (3)| 00:00:02 |
    Note
       - dynamic sampling used for this statement
    Statistics
            556  recursive calls
          46075  db block gets
           1558  consistent gets
              0  physical reads
       13575764  redo size
            924  bytes sent via SQL*Net to client
            965  bytes received via SQL*Net from client
              6  SQL*Net roundtrips to/from client
              6  sorts (memory)
              0  sorts (disk)
          42511  rows processed
    SCOTT @ jcave102 Local> alter table all_obj_cpy nologging;
    Table altered.
    Elapsed: 00:00:00.01
    SCOTT @ jcave102 Local> update all_obj_cpy
      2  set last_ddl_time = sysdate - 2;
    42511 rows updated.
    Elapsed: 00:00:01.32
    Execution Plan
    Plan hash value: 1645016300
    | Id  | Operation          | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | UPDATE STATEMENT   |             | 43776 |   384K|   137   (3)| 00:00:02 |
    |   1 |  UPDATE            | ALL_OBJ_CPY |       |       |            |          |
    |   2 |   TABLE ACCESS FULL| ALL_OBJ_CPY | 43776 |   384K|   137   (3)| 00:00:02 |
    Note
       - dynamic sampling used for this statement
    Statistics
            561  recursive calls
          44949  db block gets
           1496  consistent gets
              0  physical reads
       12799600  redo size
            924  bytes sent via SQL*Net to client
            965  bytes received via SQL*Net from client
              6  SQL*Net roundtrips to/from client
              6  sorts (memory)
              0  sorts (disk)
          42511  rows processedSecond, if you did manage to do an unlogged operation, make absolutely certain you understand the recovery implications. You must do a complete backup of the database after an unlogged operation or the table will not be recovered in the event of a database failure. If you have a standby database for disaster recovery, unlogged operations would cause the standby to be useless (hence the option to force logging at the tablespace and/or database level).
    While it's certainly possible that this is an Oracle server configuration problem, it would be relatively tough to configure a system so that a 30,000 row update would force excessive log switches. If it were a configuration problem, you'd expect that any update of 30,000 rows would be slow and that multiple sessions running smaller updates would also be slow, but I don't believe that describes the symptoms the original poster is concerned about.
    Justin

  • ADD NEW VALUE IN TO TEXTAREA IN CASE OF UPDATE FORM .

    HI,
    I HAVE A DUMMY TABLE
    CREATE TABLE  "DUMMY_MED_LITS"
       (     "MED_CODE" VARCHAR2(100),
         "MED_DOS" VARCHAR2(4000)
       ).I AM GOING TO FETCH THESE TWO COLUMNS VALUE OF DUMMY TABLE INTO TEXTAREA .TEXT AREA ALREADY HAVE VALUE I WANT TO MARGE TEXTAREA VALUE WITH THE MED_CODE AND MED_DOS VALUE .
    declare
    p varchar2(200);
    rec DUMMY_MED_LITS%ROWTYPE;
    cursor c1 is select * from DUMMY_MED_LITS;
    begin
    open c1;
    loop
    fetch c1 into rec;
    exit when c1%notfound;
    :P13_MED_COUNT:=rec.MED_CODE ||'-'||rec.MED_DOS|| CHR(13) || CHR(10);
    end loop;
    close c1;
    end;* :P13_MED_COUNT THIS IS TEXT AREA ITEM .
    BEGIN
    INSERT INTO DUMMY_MED_LITS VALUES(:P13_MED_NAME,:P13_MED_DOS);
    END;
    select DISEASE,SYMPTOMS,TRET_NAME,MED_COUNT,MED_DAYS into :P13_DISEASE,:P13_SYMPTOMS,:P13_TREATMENT,:P13_MED_COUNT,:P13_DOS_DAYS FROM PATIENT_SYMPTOMS where ID =:REQUEST;
    begin
    update PATIENT_SYMPTOMS set DISEASE=:P13_DISEASE,
    SYMPTOMS=:P13_SYMPTOMS,TRET_NAME=:P13_TREATMENT,MED_NAME=:P13_MEDICINE,
    MED_COUNT =:P13_MED_COUNT,MED_DAYS=:P13_DOS_DAYS where id =:P13_id;
    end;HOW CAN I FETCH VALUE FROM DUMMY_MED_LITS INTO TEXT AREA .IN MODIFY CASE OF UPDATION.
    THANKS
    MANOJ KAUSHIK
    Edited by: Manoj Kaushik on Apr 22, 2010 2:28 AM
    Edited by: Manoj Kaushik on Apr 22, 2010 2:29 AM

    Manoj:
    What phase of page processing are the statements that you list executed ?
    What are the conditions under which these statements executed ?
    varad

  • UPDATE statement in Receiver JDBC adapter

    Hi all,
    I would like to use UPDATE statement in my receiver JDBC adapter and would like to know how this UPDATE statement works in following case.
    1) If i have 10 records to be updated in database, whether Commit happens at the end of all 10 records updation OR it will be for every record update?
    Regards

    Hi,
    Then let me construct my query this way...
    i have 10 records that needs to be updated to database, say For Ex: 7 records are updated successfully and 8th record has issue in updating the table.
    In this case i would like to ROLLBACK entire 10 records with out committing any thing.
    So if i use your 2nd option, it should perform as i expected right?
    Regards

  • JDBC adapter - update statement

    I have come to the conclusion that there is no direct connection between the select and update statement of a sender JDBC adapter, in terms of commit scope.
    According to SAP documentation:
    "The UPDATE statement must alter exactly those data records that have been selected by the SELECT statement. You can ensure this is the case by using an identical WHERE clause. (See Processing Parameters, SQL Statement for Query, and SQL Statement for Update below)."
    But my point is: if select statement retrieves e.g. 5 rows based on a where condition, then the update statement could find 6 rows to update, if a row was inserted a split second after the select, but before the update. Result : a row is lost...
    I don't think the select statement puts a lock on the table(s) it accesses, and releases this lock after update has been committed. This would ensure integrity between select and update statement.
    Can anybody confirm or deny this ?

    Hi,
    Have you seen the<b> Isolation level for Transaction handling</b> in the sender JDBC adapter?
    Make the Isolation level as Serializable and repeatable Read and the db gets locked  anbd until Update happens, no Insertion can occur in the Split Second!
    http://help.sap.com/saphelp_nw04/helpdata/en/7e/5df96381ec72468a00815dd80f8b63/content.htm
    Regards,
    Bhavesh

Maybe you are looking for