Oracle SQL: variables used in place of table names
- You need to have a space between the table name and the subsequent
WHERE
clause - The
INTO
needs to be part of theEXECUTE IMMEDIATE
, not part of the dynamic SQL statement. - The dynamic SQL statement should not have a trailing semicolon
- 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.
INTO part of the query should not be directly included in the querystring.
Syntax
EXECUTE IMMEDIATE(<SQL>)[INTO<variable>][USING <bind_variable_value>]
The above syntax shows EXECUTE IMMEDIATE command.
Clause INTO is optional and used only if the dynamic SQL contains a select statement that fetches values. The variable type should match with the variable type of the select statement.
Clause USING is optional and used only if the dynamic SQL contains any bind variable.
https://www.guru99.com/dynamic-sql-pl-sql.html#2
You can visit this site for a better understanding of Dynamic SQL.