Oracle SQL: variables used in place of table names

后端 未结 1 372
灰色年华
灰色年华 2020-12-10 14:31

I am converting a MSSQL script to Oracle, and I haven\'t been able to figure out the syntax to use a variable in place of a table name or column.

Here is a simple ex

相关标签:
1条回答
  • 2020-12-10 15:11
    1. You need to have a space between the table name and the subsequent WHERE clause
    2. The INTO needs to be part of the EXECUTE IMMEDIATE, not part of the dynamic SQL statement.
    3. The dynamic SQL statement should not have a trailing semicolon
    4. The EXECUTE IMMEDIATE statement should end with a semicolon

    Putting those together, something like this should work

    declare 
      VR_TABLE VARCHAR2(256);
      VR_UPDATE VARCHAR2(256);
    begin
      VR_TABLE :='SYSTEM_STATUS';
      EXECUTE IMMEDIATE 'select UPDATE_VERSION from ' || VR_TABLE || ' where rownum < 2'
                   INTO VR_UPDATE;
    end;
    

    Of course, since you're not doing anything with VR_UPDATE, nothing will be displayed when this anonymous block is executed.

    0 讨论(0)
提交回复
热议问题