今天想看一下scott用户下的sql的执行计划的,可是在PL/SQL的command窗口中输入set autotrace on时,却报Cannot SET AUTOTRACE的错误,后来查阅了很多网上的说法,最终得到下面的一些经验。
首先,我去sys用户中查看了plustrace角色,发现并不存在;
然后问题就很明确了,创建出来plustrace这个角色,并赋给scott用户就好了,具体的步骤如下:
1、登陆sys用户,并执行utlxplan.sql脚本,创建plan表(以下路径为我本机的路径);
SQL> @D:\app\zl\product\11.2.0\dbhome_1\RDBMS\ADMIN\utlxplan.sql
Table created
2、还是在sys用户下,执行plustrce.sql脚本,数据库启用autotrace功能的安装脚本,主要是创建plustrace角色,并且授权 ,拥有plustrace最主要的目的是为了访问下面几个VIEW :v_$sesstat;v_$statname;v_$mystat;
SQL> @D:\app\zl\product\11.2.0\dbhome_1\sqlplus\admin\plustrce.sql
drop role plustrace;
drop role plustrace
ORA-01919: 角色 'PLUSTRACE' 不存在
create role plustrace;
Role created
grant select on v_$sesstat to plustrace;
Grant succeeded
grant select on v_$statname to plustrace;
Grant succeeded
grant select on v_$mystat to plustrace;
Grant succeeded
grant plustrace to dba with admin option;
Grant succeeded
set echo off
3、将角色plustrace赋予scott用户;
SQL> grant plustrace to scott;
Grant succeeded
在做完上述操作后,我去scott用户试了一下,发现还是报错Cannot SET AUTOTRACE,一下子完全不知道问题出在哪里了。
后来我又查阅了很多文章,发现有说set autotrace on这个命令必须在sql*plus中运行,在PL/SQL中会报错;
试了一下,果然是这样,必须要在sql*plus中运行才有效;