Click here to Skip to main content
15,921,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've a table with fields:
SQL
create table test
( f1 varchar2(10),
f2 varchar2(10),
f3 varchar3(10)
)

insert into test values ('d1','d2','d3');
insert into test values ('d10','d20','d30');


I want to update the fields of the table as per need i.e update only one field leaving all the data of the fields as it is.
How can I do it
? Suppose I want to update only f1 (from d1 to x1) field leaving f2, and f3 as it is. I've written stored procedure to update all the fields but do not know how to do it??
SQL
CREATE OR REPLACE PROCEDURE UPDATE_TEST
( U_F1 TEST.F1%TYPE,
U_F2 TEST.F2%TYPE,
U_F3 TEST.F3%TYPE
)
IS
BEGIN
    UPDATE TEST SET F1 = U_F1,F2 = U_F2 WHERE F3 = U_F3;

    COMMIT;
  END;
Posted

1 solution

Try this:

SQL
CREATE OR REPLACE PROCEDURE Update_Test (
   u_f1_ test.f1%TYPE DEFAULT NULL,
   u_f2_ test.f2%TYPE DEFAULT NULL,
   u_f3_ test.f3%TYPE DEFAULT NULL )
IS
BEGIN
   UPDATE test 
      SET f1 = NVL(u_f1_, f1),
          f2 = NVL(u_f2_, f2),
          f3 = NVL(u_f3_, f3);
   COMMIT;
END Update_Test;


You can pass NULL to the fields you do not want to update.

Few side notes on coding style:
1. See how I have used variable names and table column names. I have used an underscore to end variable names, but no underscore to table column names. It's not a must, but I have found it easier to distinguish between variables and table columns.
2. See how each assignment in the SET clause of update statement is in a new line. I find this style more clearer.
3. See the capitalization: ALLCAPS for keywords/reserved words, SentenceCase for procedure names, simples for the rest. Oh, I use ALLCAPS to things like NVL, MAX, COUNT, ...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900