简体   繁体   中英

Variable inclause is not working for the PL/SQL

I have this PL/SQL piece of code RESTALLSEQUENCENUMBERLIST contains value '2,3,4' which i am converting this into '2','3','4' for the in clause in the third line by the second statement. But my update query is giving me invalid number. Can anybody help with this

restAllSequenceNumberInList varchar2(100);

SELECT 
'''' || REPLACE( RESTALLSEQUENCENUMBERLIST, ',', ''',''' ) || '''' 
into restAllSequenceNumberInList FROM DUAL;

UPDATE THIRD_PARTY_LOOKUP 
SET ADDRESS_ID = firstSequenceNumber 
WHERE ADDRESS_ID in  (RESTALLSEQUENCENUMBERINLIST);

The in clause does not take a string argument that represents multiple values. It is as simple as that. If you pass a single string to in it behaves the same as = .

You can do what you want using like :

UPDATE THIRD_PARTY_LOOKUP 
    SET ADDRESS_ID = firstSequenceNumber 
    WHERE ','||ADDRESS_ID||',' LIKE '%,' || RESTALLSEQUENCENUMBERLIST  || '%,';

However, instead of storing lists in a string, why not store them in a temporary table? After all, tables are the SQL construct designed explicitly for storing lists of things.

You cannot pass comma separated list in a variable and assume SQL to treat them as separate values, try this

--replace with " not '
SELECT 
REPLACE '''' ||  REPLACE( RESTALLSEQUENCENUMBERLIST, ',', '","' ) || '''' 
into restAllSequenceNumberInList FROM DUAL;

--use xml to split them into rows
UPDATE THIRD_PARTY_LOOKUP 
SET ADDRESS_ID = firstSequenceNumber 
WHERE ADDRESS_ID in 
(SELECT EXTRACTVALUE(COLUMN_VALUE,'text()') VALS 
 FROM XMLTABLE(restAllSequenceNumberInList) )

This is not possible using one single string containing the list. You need a list generated from splitting the string using one way or another. Unfortunately there exists no "easy" split function. Several methods exist.

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