How do I use a record that has dots stored in it without throwing errors? (In PLSQL) [duplicate]

StackOverflow https://stackoverflow.com/questions/21857331

  •  13-10-2022
  •  | 
  •  

Question

I have the following loop in a procedure to scramble PII. The issue is that the IV_USER is stored as James.T.Sanders with dots withing the name. This is causing me to get ORA-00904: "James"."T"."Sanders": invalid identifier

Is there a way around this?

        OPEN user_list FOR 'select IV_USER FROM EXT_USER.PORTAL_ACCOUNTS';
        LOOP
        FETCH user_list INTO user_list_rec;
            execute immediate 'update EXT_USER.PORTAL_ACCOUNTS set PERSON_ID=:ssan_new WHERE IV_USER='||user_list_rec.IV_USER||' ' USING ssan_new;
            ssan_new := ssan_new + 1;

        END LOOP;

If any more clarification is needed please let me know.

Was it helpful?

Solution

I believe the assignment in the WHERE condition missed Single Quotes!

As the Query is framed a String, assigning a VARCHAR variable just paste the value like WHERE IV_USER = James.T.Sanders. And it has to be either used as a bind variable or enclosed within single quotes !

    OPEN user_list FOR 'select IV_USER FROM EXT_USER.PORTAL_ACCOUNTS';
    LOOP
    FETCH user_list INTO user_list_rec;
        execute immediate 'update EXT_USER.PORTAL_ACCOUNTS set PERSON_ID=:ssan_new WHERE IV_USER='''||user_list_rec.IV_USER||''' ' USING ssan_new;
        ssan_new := ssan_new + 1;

    END LOOP;

OR (Without a Dynamic SQL for UPDATE, Ofcourse many solutions are possible without dynamic SQL)

    OPEN user_list FOR 'select IV_USER FROM EXT_USER.PORTAL_ACCOUNTS';
    LOOP
    FETCH user_list INTO user_list_rec;
        update EXT_USER.PORTAL_ACCOUNTS set PERSON_ID = ssan_new WHERE IV_USER = user_list_rec.IV_USER;
        ssan_new := ssan_new + 1;

    END LOOP;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top