[转载]【原创】set autotrace on 报

一、问题描述:

    今天想看一下SQL的执行计划,在PL/SQL的command窗口中输入set autotrace on时,报Cannot SET AUTOTRACE的错误。

二、解决办法:

    这个命令必须在sql*plus中运行,在PL/SQL中会报错

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:UsersAdministrator>sqlplus scott/tiger

SQL*Plus: Release 11.2.0.3.0 Production on 星期三 11月 20 11:16:52 2013

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


连接到:
Personal Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> set autotrace on;
SQL> select * from emp;

三、分析:

在网搜解决方法时,发现即使在sql*plus中也有可能出现这个问题,虽然没有遇到,但是将这种情况下的解决方法先记下来备用:

1.首先必须采用用Oracle的sqlplus登陆sys账号

sqlplus " sys/sys@XXX as sysdba "

2.然后执行如下脚本:

(1)plustrce.sql (创建plustrace角色并授权)   脚本内容如下:

-------------------

set echo on

drop role plustrace;
create role plustrace;

grant select on v_$sesstat to plustrace;
grant select on v_$statname to plustrace;
grant select on v_$mystat to plustrace;
grant plustrace to dba with admin option;

set echo off

-------------------

(2)utlxplan.sql (创建执行计划的表)脚本内容如下:

--------------------

create table PLAN_TABLE (
        statement_id       varchar2(30),
        plan_id            number,
        timestamp          date,
        remarks            varchar2(4000),
        operation          varchar2(30),
        options            varchar2(255),
        object_node        varchar2(128),
        object_owner       varchar2(30),
        object_name        varchar2(30),
        object_alias       varchar2(65),
        object_instance    numeric,
        object_type        varchar2(30),
        optimizer          varchar2(255),
        search_columns     number,
        id                 numeric,
        parent_id          numeric,
        depth              numeric,
        position           numeric,
        cost               numeric,
        cardinality        numeric,
        bytes              numeric,
        other_tag          varchar2(255),
        partition_start    varchar2(255),
        partition_stop     varchar2(255),
        partition_id       numeric,
        other              long,
        distribution       varchar2(30),
        cpu_cost           numeric,
        io_cost            numeric,
        temp_space         numeric,
        access_predicates  varchar2(4000),
        filter_predicates  varchar2(4000),
        projection         varchar2(4000),
        time               numeric,
        qblock_name        varchar2(30),
        other_xml          clob
);

--------------------

3.然后将该角色和对表的操作权限赋给用户

 grant all on plan_table to scott; 

 grant plustrace to scott; 

原文地址:https://www.cnblogs.com/liuzhuqing/p/7480295.html