PL/SQL: SELECT INTO using a variable in the FROM clause











up vote
0
down vote

favorite












Code:



lc_tab1_col1 VARCHAR2(4000);
lc_tab1_col2 VARCHAR2(4000);
lc_tab2_col2 VARCHAR2(4000);
lc_tab2_col2 VARCHAR2(4000);
CURSOR my_cursor IS select col1, col2 from tab1;

[...]

OPEN my_cursor;

LOOP
FETCH my_cursor INTO lc_tab1_col1, lc_tab1_col2;
EXIT WHEN my_cursor%NOTFOUND;

SELECT lc_tab1_col2.col1, lc_tab1_col2.col2 INTO lc_tab2_col2, lc_tab2_col2 FROM lc_tab1_col2 WHERE lc_tab1_col2.col3 = lc_tab1_col1;


[...]

END LOOP;

CLOSE my_cursor;


Hey folks,



I am trying to get the above code working.
The issue I am having is that a SELECT INTO statement apparently does not support using a variable (in that case lc_tab1_col2) as the table name in the FROM clause of the statement.



When compiling the package an ORA-000942 is thrown (table or view does not exist), which tells me the variable is interpreted directly instead of being replaced and interpreted at runtime.



I can't think of a workaround on the fly, any ideas on how to fix this?



Some more background: lc_tab1_col2 contains the name of a table in the database whereas lc_tab1_col1 contains an ID.



This ID is present in all of the tables that can be contained in lc_tab1_col2 (hence the WHERE clause).



Apart from the ID there are two other columns (lc_tab1_col2.col1 and lc_tab1_col2.col2) that are present in all those tables, but that are not present in tab1. I need to select those two values to work with them inside the loop.



As there are many tables to consider, I need this SELECT INTO statement to be dynamic. It wouldn't be feasible to parse the tables one by one. Looking forward to anyone sharing a clever idea for overcoming this issue :) Thanks in advance!










share|improve this question




















  • 2




    for dynamic SQL you can use execute immediate. You can't provide table name from a variable directly
    – Gurwinder Singh
    Nov 19 at 13:34












  • I only used execute immediate to execute DDL statements up till now. Can you actually execute a SELECT statement with it and save the result to one (or more) variables?!
    – daZza
    Nov 19 at 13:37








  • 1




    Yes of course, see this link
    – Gurwinder Singh
    Nov 19 at 13:38















up vote
0
down vote

favorite












Code:



lc_tab1_col1 VARCHAR2(4000);
lc_tab1_col2 VARCHAR2(4000);
lc_tab2_col2 VARCHAR2(4000);
lc_tab2_col2 VARCHAR2(4000);
CURSOR my_cursor IS select col1, col2 from tab1;

[...]

OPEN my_cursor;

LOOP
FETCH my_cursor INTO lc_tab1_col1, lc_tab1_col2;
EXIT WHEN my_cursor%NOTFOUND;

SELECT lc_tab1_col2.col1, lc_tab1_col2.col2 INTO lc_tab2_col2, lc_tab2_col2 FROM lc_tab1_col2 WHERE lc_tab1_col2.col3 = lc_tab1_col1;


[...]

END LOOP;

CLOSE my_cursor;


Hey folks,



I am trying to get the above code working.
The issue I am having is that a SELECT INTO statement apparently does not support using a variable (in that case lc_tab1_col2) as the table name in the FROM clause of the statement.



When compiling the package an ORA-000942 is thrown (table or view does not exist), which tells me the variable is interpreted directly instead of being replaced and interpreted at runtime.



I can't think of a workaround on the fly, any ideas on how to fix this?



Some more background: lc_tab1_col2 contains the name of a table in the database whereas lc_tab1_col1 contains an ID.



This ID is present in all of the tables that can be contained in lc_tab1_col2 (hence the WHERE clause).



Apart from the ID there are two other columns (lc_tab1_col2.col1 and lc_tab1_col2.col2) that are present in all those tables, but that are not present in tab1. I need to select those two values to work with them inside the loop.



As there are many tables to consider, I need this SELECT INTO statement to be dynamic. It wouldn't be feasible to parse the tables one by one. Looking forward to anyone sharing a clever idea for overcoming this issue :) Thanks in advance!










share|improve this question




















  • 2




    for dynamic SQL you can use execute immediate. You can't provide table name from a variable directly
    – Gurwinder Singh
    Nov 19 at 13:34












  • I only used execute immediate to execute DDL statements up till now. Can you actually execute a SELECT statement with it and save the result to one (or more) variables?!
    – daZza
    Nov 19 at 13:37








  • 1




    Yes of course, see this link
    – Gurwinder Singh
    Nov 19 at 13:38













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Code:



lc_tab1_col1 VARCHAR2(4000);
lc_tab1_col2 VARCHAR2(4000);
lc_tab2_col2 VARCHAR2(4000);
lc_tab2_col2 VARCHAR2(4000);
CURSOR my_cursor IS select col1, col2 from tab1;

[...]

OPEN my_cursor;

LOOP
FETCH my_cursor INTO lc_tab1_col1, lc_tab1_col2;
EXIT WHEN my_cursor%NOTFOUND;

SELECT lc_tab1_col2.col1, lc_tab1_col2.col2 INTO lc_tab2_col2, lc_tab2_col2 FROM lc_tab1_col2 WHERE lc_tab1_col2.col3 = lc_tab1_col1;


[...]

END LOOP;

CLOSE my_cursor;


Hey folks,



I am trying to get the above code working.
The issue I am having is that a SELECT INTO statement apparently does not support using a variable (in that case lc_tab1_col2) as the table name in the FROM clause of the statement.



When compiling the package an ORA-000942 is thrown (table or view does not exist), which tells me the variable is interpreted directly instead of being replaced and interpreted at runtime.



I can't think of a workaround on the fly, any ideas on how to fix this?



Some more background: lc_tab1_col2 contains the name of a table in the database whereas lc_tab1_col1 contains an ID.



This ID is present in all of the tables that can be contained in lc_tab1_col2 (hence the WHERE clause).



Apart from the ID there are two other columns (lc_tab1_col2.col1 and lc_tab1_col2.col2) that are present in all those tables, but that are not present in tab1. I need to select those two values to work with them inside the loop.



As there are many tables to consider, I need this SELECT INTO statement to be dynamic. It wouldn't be feasible to parse the tables one by one. Looking forward to anyone sharing a clever idea for overcoming this issue :) Thanks in advance!










share|improve this question















Code:



lc_tab1_col1 VARCHAR2(4000);
lc_tab1_col2 VARCHAR2(4000);
lc_tab2_col2 VARCHAR2(4000);
lc_tab2_col2 VARCHAR2(4000);
CURSOR my_cursor IS select col1, col2 from tab1;

[...]

OPEN my_cursor;

LOOP
FETCH my_cursor INTO lc_tab1_col1, lc_tab1_col2;
EXIT WHEN my_cursor%NOTFOUND;

SELECT lc_tab1_col2.col1, lc_tab1_col2.col2 INTO lc_tab2_col2, lc_tab2_col2 FROM lc_tab1_col2 WHERE lc_tab1_col2.col3 = lc_tab1_col1;


[...]

END LOOP;

CLOSE my_cursor;


Hey folks,



I am trying to get the above code working.
The issue I am having is that a SELECT INTO statement apparently does not support using a variable (in that case lc_tab1_col2) as the table name in the FROM clause of the statement.



When compiling the package an ORA-000942 is thrown (table or view does not exist), which tells me the variable is interpreted directly instead of being replaced and interpreted at runtime.



I can't think of a workaround on the fly, any ideas on how to fix this?



Some more background: lc_tab1_col2 contains the name of a table in the database whereas lc_tab1_col1 contains an ID.



This ID is present in all of the tables that can be contained in lc_tab1_col2 (hence the WHERE clause).



Apart from the ID there are two other columns (lc_tab1_col2.col1 and lc_tab1_col2.col2) that are present in all those tables, but that are not present in tab1. I need to select those two values to work with them inside the loop.



As there are many tables to consider, I need this SELECT INTO statement to be dynamic. It wouldn't be feasible to parse the tables one by one. Looking forward to anyone sharing a clever idea for overcoming this issue :) Thanks in advance!







sql oracle plsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 13:34

























asked Nov 19 at 13:26









daZza

1,1931741




1,1931741








  • 2




    for dynamic SQL you can use execute immediate. You can't provide table name from a variable directly
    – Gurwinder Singh
    Nov 19 at 13:34












  • I only used execute immediate to execute DDL statements up till now. Can you actually execute a SELECT statement with it and save the result to one (or more) variables?!
    – daZza
    Nov 19 at 13:37








  • 1




    Yes of course, see this link
    – Gurwinder Singh
    Nov 19 at 13:38














  • 2




    for dynamic SQL you can use execute immediate. You can't provide table name from a variable directly
    – Gurwinder Singh
    Nov 19 at 13:34












  • I only used execute immediate to execute DDL statements up till now. Can you actually execute a SELECT statement with it and save the result to one (or more) variables?!
    – daZza
    Nov 19 at 13:37








  • 1




    Yes of course, see this link
    – Gurwinder Singh
    Nov 19 at 13:38








2




2




for dynamic SQL you can use execute immediate. You can't provide table name from a variable directly
– Gurwinder Singh
Nov 19 at 13:34






for dynamic SQL you can use execute immediate. You can't provide table name from a variable directly
– Gurwinder Singh
Nov 19 at 13:34














I only used execute immediate to execute DDL statements up till now. Can you actually execute a SELECT statement with it and save the result to one (or more) variables?!
– daZza
Nov 19 at 13:37






I only used execute immediate to execute DDL statements up till now. Can you actually execute a SELECT statement with it and save the result to one (or more) variables?!
– daZza
Nov 19 at 13:37






1




1




Yes of course, see this link
– Gurwinder Singh
Nov 19 at 13:38




Yes of course, see this link
– Gurwinder Singh
Nov 19 at 13:38












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I think, you exception really means that this table does not exist or you don't have privileges to SELECT it.



I've executed a below code and everything was ok. I have tried to compile it in a package and also I didn't have any compilation errors



DECLARE 
user_tables varchar2(30) := 'TBLCOMPANIES';
BEGIN
SELECT table_name
INTO user_tables
FROM user_tables
WHERE user_tables.table_name = user_tables;

dbms_output.put_line(user_tables) ;
END;
/





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375646%2fpl-sql-select-into-using-a-variable-in-the-from-clause%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I think, you exception really means that this table does not exist or you don't have privileges to SELECT it.



    I've executed a below code and everything was ok. I have tried to compile it in a package and also I didn't have any compilation errors



    DECLARE 
    user_tables varchar2(30) := 'TBLCOMPANIES';
    BEGIN
    SELECT table_name
    INTO user_tables
    FROM user_tables
    WHERE user_tables.table_name = user_tables;

    dbms_output.put_line(user_tables) ;
    END;
    /





    share|improve this answer

























      up vote
      0
      down vote













      I think, you exception really means that this table does not exist or you don't have privileges to SELECT it.



      I've executed a below code and everything was ok. I have tried to compile it in a package and also I didn't have any compilation errors



      DECLARE 
      user_tables varchar2(30) := 'TBLCOMPANIES';
      BEGIN
      SELECT table_name
      INTO user_tables
      FROM user_tables
      WHERE user_tables.table_name = user_tables;

      dbms_output.put_line(user_tables) ;
      END;
      /





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I think, you exception really means that this table does not exist or you don't have privileges to SELECT it.



        I've executed a below code and everything was ok. I have tried to compile it in a package and also I didn't have any compilation errors



        DECLARE 
        user_tables varchar2(30) := 'TBLCOMPANIES';
        BEGIN
        SELECT table_name
        INTO user_tables
        FROM user_tables
        WHERE user_tables.table_name = user_tables;

        dbms_output.put_line(user_tables) ;
        END;
        /





        share|improve this answer












        I think, you exception really means that this table does not exist or you don't have privileges to SELECT it.



        I've executed a below code and everything was ok. I have tried to compile it in a package and also I didn't have any compilation errors



        DECLARE 
        user_tables varchar2(30) := 'TBLCOMPANIES';
        BEGIN
        SELECT table_name
        INTO user_tables
        FROM user_tables
        WHERE user_tables.table_name = user_tables;

        dbms_output.put_line(user_tables) ;
        END;
        /






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 16:17









        RGruca

        263




        263






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375646%2fpl-sql-select-into-using-a-variable-in-the-from-clause%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft