简体   繁体   中英

SQL update with PL/sql function

I have to update one column in my table ( the requirement to use function). I have created simple function for customer1 table. So how I can update my table using function . Is there any ways ?

CREATE OR REPLACE FUNCTION fn_easy (name_in in customer1.last_name%type)          
   RETURN customer1.first_name%type IS
   name_tab customer1.first_name%type;
        BEGIN
  select first_name  into name_tab  from  customer1
  where last_name  = name_in;
     RETURN name_tab;
  END fn_easy;

 update customer1
 set first_name  = fn_easy(customer1.last_name);

I understand that we need to loop argument in function. The only one idea which I have is to do it through the cursor. But I don't think it is optimal to use cursor for this task when I have 200 records.

Your function is generally ok, but you have to ensure the select inside returns exactly 1 value, otherwise you will get an exception. If you want to select all values into an array, use BULK COLLECT INTO . To update a column value using a function, the column value type must be compatible with function return type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM