形成动态PLSQL时出错

时间:2015-01-28 14:40:20

标签: oracle plsql

我正在尝试在SQL语句下面形成。但得到这个奇怪的错误,我似乎无法搞清楚。我已经在循环外单独执行了每个语句,它们工作正常。请有人帮我找错。

错误

ORA-06550: line 6, column 14:
PLS-00306: wrong number or types of arguments in call to '||'
ORA-06550: line 6, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

PLSQL声明:

 DECLARE
 L_sql VARCHAR2(2000):=NULL;
BEGIN      
FOR val IN (SELECT generation_qtr from test_1) 
  LOOP
      L_sql:=L_sql ||' MAX(DECODE(generation_qtr, '||''''||val||''''||' cum_actual_gen)) AS ' || val ||','||chr(10);
  END LOOP;
  L_sql:='SELECT assetname, '|| L_sql;
  L_sql:=substr(L_sql,1,LENGTH(L_sql)-1);
dbms_output.put_line(L_sql);
END;

Oracle Version -11.2

1 个答案:

答案 0 :(得分:4)

您直接引用val,但这是游标行类型。您需要指定列名称,即使只有一个:

... ||val.generation_qtr|| ...

...在两个地方你都在连接中使用它。所以它变成了:

      L_sql:=L_sql ||' MAX(DECODE(generation_qtr, '||''''
        || val.generation_qtr ||''''||' cum_actual_gen)) AS '
        || val.generation_qtr ||','||chr(10);

如果您要动态执行此操作,那么新行字符将不会非常有用,但我猜它们有助于调试。