简体   繁体   中英

SQL Developer ORACLE SCRPT

I have a table with 2 columns

Common name usr_name
ABC 123 123
CDE 123 XYZ 123

I'm trying to find a PL SQL script that would go through all the users in the common name column and if the Common name not equal usr_name then it updates the usr name and makes it the same as common name

should look like this

if common name != usr name // update usr_name == common name

You do not need PL/SQL, use an UPDATE SQL statement and put your logic in the WHERE clause:

UPDATE table_name
SET   usr_name = common_name
WHERE usr_name != common_name
OR    (usr_name IS NOT NULL AND common_name IS NULL)
OR    (usr_name IS NULL AND common_name IS NOT NULL)

Which, for the sample data:

CREATE TABLE table_name (Common_name, usr_name) AS
SELECT 'ABC 123', '123' FROM DUAL UNION ALL
SELECT 'CDE 123', 'XYZ 123' FROM DUAL

Then, after the update the table contains:

COMMON_NAME USR_NAME
ABC 123 ABC 123
CDE 123 CDE 123

db<>fiddle here

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