For reasons that aren't immediately clear to me, the problem is that the variable you're using is nvarchar2
. It works if you declare it as varchar2
:
curTableName VARCHAR2(50);
Validated via SQL Fiddle; that fiddle fails if you just change the declaration of the table name variable to nvarchar2
.
As @jonearles points out, the documentation does state that the command string's 'type must be either CHAR
, VARCHAR2
, or CLOB
'. And although the documentation for the concatenation operator doesn't refer to it, the related concat
function does state that 'if one of the arguments is a national data type, then the returned value is a national data type' - so your command string is nvarchar2
because the variable you're using in the concatenation is nvarchar2
, making the argument to the execute immediate
statement illegal.
It's not a great idea to create tables on the fly though. Schema objects should normally be created once. You could have a table - possibly a global temporay table - with a month column, or at a push twelve tables that you empty and populate as needed.
Also, all your select from dual
statements could be simplified:
curMonthChar := TO_CHAR(ADD_MONTHS(sysdate,-1),'fmMONTH');
curTableName := 'QP17414_'||curMonthChar);
etc., even in your subquery:
... WHERE OWNER = user AND ...
... and I have no idea why you're using so much concatenation in your pivot clause.