-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathload.sql
265 lines (219 loc) · 7.56 KB
/
load.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
set feed off;
whenever sqlerror exit;
prompt ~~~~~~~~~~
prompt AWR LOAD
prompt ~~~~~~~~~~
prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prompt ~ This script will load the AWR data from a dump file. The ~
prompt ~ script will prompt users for the following information: ~
prompt ~ (1) name of directory object ~
prompt ~ (2) name of dump file ~
prompt ~ (3) staging schema name to load AWR data into ~
prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
-- Ask User for Directory Name
--
prompt
prompt Specify the Directory Name
prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~
col owner for a30;
col dir_name for a30;
col dir_path for a120;
select
owner
, directory_name as dir_name
, directory_path as dir_path
FROM dba_directories
/
variable dmpdir varchar2(30);
variable dmppath varchar2(30);
variable dmpfile varchar2(30);
accept _dmpdir prompt "Enter directory name: ";
declare
lv_pattern varchar2(1024);
lv_ns varchar2(1024);
begin
:dmpdir := q'[&_dmpdir]';
select directory_path
into lv_pattern
from dba_directories
where directory_name = '&_dmpdir';
sys.dbms_backup_restore.searchfiles(lv_pattern, lv_ns);
end;
/
SELECT indx, FNAME_KRBMSFT AS file_name
FROM X$KRBMSFT;
prompt;
accept _dmpfile prompt "Please specify the prefix of the dump file (.dmp) to load(without path): ";
begin
:dmpfile := q'[&_dmpfile]';
end;
/
---------------------------------------------------------
-- Original Stage schema creation:
set termout off;
column dflt_schema new_value dflt_schema noprint;
select 'AWR_STAGE' dflt_schema from dual;
set termout on;
prompt
prompt Staging Schema to Load AWR Snapshot Data
prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prompt The next step is to create the staging schema
prompt where the AWR snapshot data will be loaded.
prompt After loading the data into the staging schema,
prompt the data will be transferred into the AWR tables
prompt in the SYS schema.
prompt
prompt
prompt The default staging schema name is &dflt_schema..
prompt To use this name, press <return> to continue, otherwise enter
prompt an alternative.
prompt
set heading off;
column schema_name new_value schema_name noprint;
column schema_password new_value schema_password noprint;
select 'Using the staging schema name: ' || nvl('&&schema_name','&dflt_schema')
, nvl('&&schema_name','&dflt_schema') schema_name
, substr(nvl('&&schema_name','&dflt_schema'),1,2) || '$999$'
|| substr(rawtohex(sys_guid()),11,10) || '$_#zzz$' schema_password
from sys.dual;
variable schname varchar2(30);
variable schcount number;
/* check if schema already exists */
declare
cursor schemas (schname varchar2) is
select count(*) schcount
from dba_users
where username = schname
order by username;
begin
:schname := '&schema_name';
/* select the directory path into a variable */
open schemas(:schname);
fetch schemas into :schcount;
if (:schcount > 0) then
RAISE_APPLICATION_ERROR(-20104,
'schema name ''' || :schname ||
''' already exists', TRUE);
end if;
close schemas;
end;
/
prompt
prompt Choose the Default tablespace for the &schema_name user
prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prompt Choose the &schema_name users`s default tablespace. This is the
prompt tablespace in which the AWR data will be staged.
set heading on
column db_default format a18 heading 'DEFAULT TABLESPACE'
select tablespace_name, contents
, decode(tablespace_name,'SYSAUX','*') db_default
from sys.dba_tablespaces
where tablespace_name <> 'SYSTEM'
and contents = 'PERMANENT'
and status = 'ONLINE'
order by tablespace_name;
set heading off
prompt
prompt Pressing <return> will result in the recommended default
prompt tablespace (identified by *) being used.
prompt
col default_tablespace new_value default_tablespace noprint
select 'Using tablespace '||
upper(nvl('&&default_tablespace','SYSAUX'))||
' as the default tablespace for the &&schema_name.'
, nvl('&default_tablespace','SYSAUX') default_tablespace
from sys.dual;
begin
if upper('&&default_tablespace') = 'SYSTEM' then
raise_application_error(-20105, 'Load failed - SYSTEM tablespace ' ||
'specified for DEFAULT tablespace');
end if;
end;
/
prompt
prompt
prompt Choose the Temporary tablespace for the &&schema_name user
prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prompt Choose the &schema_name user`s temporary tablespace.
set heading on
column db_default format a23 heading 'DEFAULT TEMP TABLESPACE'
select t.tablespace_name, t.contents
, decode(dp.property_name,'DEFAULT_TEMP_TABLESPACE','*') db_default
from sys.dba_tablespaces t
, sys.database_properties dp
where t.contents = 'TEMPORARY'
and t.status = 'ONLINE'
and dp.property_name(+) = 'DEFAULT_TEMP_TABLESPACE'
and dp.property_value(+) = t.tablespace_name
order by tablespace_name;
set heading off
prompt
prompt Pressing <return> will result in the database`s default temporary
prompt tablespace (identified by *) being used.
prompt
col temporary_tablespace new_value temporary_tablespace noprint
select 'Using tablespace '||
nvl('&&temporary_tablespace',property_value)||
' as the temporary tablespace for &&schema_name.'
, nvl('&&temporary_tablespace',property_value) temporary_tablespace
from database_properties
where property_name='DEFAULT_TEMP_TABLESPACE';
begin
if upper('&&temporary_tablespace') = 'SYSTEM' then
raise_application_error(-20106, 'Load failed - SYSTEM tablespace ' ||
'specified for TEMPORARY tablespace');
end if;
end;
/
set heading off
prompt
prompt
prompt ... Creating &&schema_name user
create user &&schema_name
identified by &&schema_password
default tablespace &&default_tablespace
temporary tablespace &&temporary_tablespace;
alter user &&schema_name quota unlimited on &&default_tablespace;
prompt
set termout on;
---------------------------------------------------------------------------
whenever sqlerror continue;
set heading off;
set linesize 110 pagesize 50000;
set echo off;
set feedback off;
set termout on;
begin
/* call PL/SQL routine to load the data into the staging schema */
dbms_swrf_internal.awr_load(schname => :schname,
dmpfile => :dmpfile,
dmpdir => :dmpdir);
end;
/
prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prompt AWR dump was succesfully loaded into stage schema;
prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pause * Press Enter to continue (next step = move_to_awr)...
-- Check DBID:
---------------------------------------------------------------------------
set heading on;
select distinct dbid, version,db_name,instance_name,host_name from dba_hist_database_instance;
accept _dbid prompt "Enter AWR dbid if you want to change it before load: " default 0;
---------------------------------------------------------------------------
begin
/* call PL/SQL routine to move the data into AWR */
if &_dbid = 0 then
dbms_swrf_internal.move_to_awr(schname => :schname);
else
dbms_swrf_internal.move_to_awr(schname => :schname,
new_dbid => &_dbid);
end if;
dbms_swrf_internal.clear_awr_dbid;
end;
/
prompt ... Dropping &&schema_name user
drop user &&schema_name cascade;
prompt
prompt End of AWR Load
set heading on lines 1500 feed on;