2

I want to execute an pl/sql script via terminal but I can't manage to get it to work. It first checks whether an user exists and if he does then it copies data from some table of that user.

Problem arises when there is no user - script doesn't work because it says that table or view does not exist, and that means it somehow precompiles it, while I want it to execute line by line.

Here it is:

DECLARE
    v_count INTEGER := 0;
BEGIN
    SELECT COUNT (1) INTO v_count FROM SYS.DBA_USERS WHERE username = UPPER ('B');

    if v_count = 0 then
        DBMS_OUTPUT.put_line ('Fail');
    else
        insert into A.some_table (some_column)
            select some_column from B.some_table
                where some_column = "x";
    end if;
END;
/

it throws error that table does not exist at line select some_column from B.some_table because while it indeed does not exist (the user does not) the script wouldn't actually go there.

0

1 Answer 1

3

You need to use dynamic PL/SQL for the insert, so that it is not validated at compile time but only at runtime:

DECLARE
    v_count INTEGER := 0;
BEGIN
    SELECT COUNT (1) INTO v_count FROM SYS.DBA_USERS WHERE username = UPPER ('B');

    if v_count = 0 then
        DBMS_OUTPUT.put_line ('Fail');
    else
        EXECUTE IMMEDIATE
        'insert into A.some_table (some_column)
            select some_column from B.some_table
                where some_column = ''x''';
    end if;
END;
/
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.