17 Copyright © 2004, Oracle. All rights reserved. Advanced Tools and Techniques
Jan 05, 2016
17Copyright © 2004, Oracle. All rights reserved.
Advanced Tools and Techniques
17-2 Copyright © 2004, Oracle. All rights reserved.
Objectives
After completing this lesson, you should be able to:
• Generate scripts using SQL queries
• Use Dynamic SQL to automate script generation
• Create an external table
• Retrieve user context variables
• Issue statements that can be suspend upon encountering space condition errors
17-3 Copyright © 2004, Oracle. All rights reserved.
Using SQL to Generate Scripts
• Query dictionary tables
• Spool output to a file
• Use SQL*Plus or iSQL*Plus settings to remove unneeded text
• Use literals and concatenation characters to create SQL commands
SET HEAD OFFSET FEEDBACK OFFSPOOL drop_user_tables.sql
SELECT 'DROP TABLE ' || table_name || ';'FROM user_tables;
17-4 Copyright © 2004, Oracle. All rights reserved.
Example: Compiling Invalid Objects
SET HEAD OFFSET FEEDBACK OFFSPOOL compile_invalid_objects.sql
SELECT 'ALTER ' || object_type || ' ' || owner||'.'||object_name||' COMPILE;'FROM DBA_OBJECTSWHERE status = 'INVALID'AND object_type NOT IN ('PACKAGE BODY','TYPE BODY', 'SYNONYM');
SELECT 'ALTER PACKAGE ' || owner || '.' || object_name || ' COMPILE BODY;'FROM DBA_OBJECTSWHERE status != 'VALID'AND object_type = 'PACKAGE BODY';
17-5 Copyright © 2004, Oracle. All rights reserved.
What Is a Procedure?
• A procedure is a type of subprogram that performs an action.
• A procedure can be stored in the database, as a schema object, for repeated execution.
CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter1 [mode1] datatype1, parameter2 [mode2] datatype2, ...)]IS | AS<PL/SQL Block>;
17-6 Copyright © 2004, Oracle. All rights reserved.
PL/SQL Procedures
Callingenvironment
Procedure
(DECLARE)
BEGIN
EXCEPTION
END;
IN parameter
OUT parameter
IN OUT parameter
17-7 Copyright © 2004, Oracle. All rights reserved.
Using IN and OUT Parameters
CREATE OR REPLACE PROCEDURE gen_rebuild_idx_script (schema IN VARCHAR2) ISBEGIN FOR idx IN (SELECT owner, index_name FROM ALL_INDEXES WHERE owner = schema) LOOP DBMS_OUTPUT.PUT_LINE('ALTER INDEX ' || idx.owner || '.' || idx.index_name || ' REBUILD;'); END LOOP;END gen_rebuild_idx_script;/
SET head OFFSET feedback OFFSPOOL rebuild_indexes.sql
EXEC gen_rebuild_idx_script('HR');
17-9 Copyright © 2004, Oracle. All rights reserved.
Using Native Dynamic SQL
Dynamic SQL:
• Is a SQL statement that contains variables that can change during runtime
• Is a SQL statement with placeholders and is stored as a character string
• Enables general-purpose code to be written
• Enables data-definition, data-control, or session-control statements to be written and executed from PL/SQL
• Is written using either DBMS_SQL or native dynamic SQL
17-10 Copyright © 2004, Oracle. All rights reserved.
The EXECUTE IMMEDIATE Statement
• INTO is used for single-row queries and specifies the variables or records into which column values are retrieved.
• USING is used to hold all bind arguments. The default parameter mode is IN.
EXECUTE IMMEDIATE dynamic_string [INTO {define_variable [, define_variable] ... | record}] [USING [IN|OUT|IN OUT] bind_argument [, [IN|OUT|IN OUT] bind_argument] ... ];
17-12 Copyright © 2004, Oracle. All rights reserved.
Dynamic SQL Using EXECUTE IMMEDIATE
CREATE PROCEDURE del_rows (p_table_name IN VARCHAR2, p_rows_deld OUT NUMBER)ISBEGIN EXECUTE IMMEDIATE 'DELETE FROM '||p_table_name; p_rows_deld := SQL%ROWCOUNT;END;/
VARIABLE deleted NUMBEREXECUTE del_rows('test_employees', :deleted)PRINT deleted
17-13 Copyright © 2004, Oracle. All rights reserved.
Dynamic SQL Example
DECLARE sql_stmt VARCHAR2(1024); CURSOR get_obj IS SELECT object_name, owner FROM ALL_OBJECTS WHERE owner = UPPER('&schema_name') AND object_type IN ('PACKAGE','FUNCTION','PROCEDURE','TYPE'); BEGIN FOR rec IN get_obj LOOP sql_stmt := 'CREATE PUBLIC SYNONYM '||rec.object_name||' FOR '||rec.owner||'.' ||rec.object_name; DBMS_OUTPUT.PUT_LINE(sql_stmt); EXECUTE IMMEDIATE sql_stmt; END LOOP;END;/
17-14 Copyright © 2004, Oracle. All rights reserved.
External Tables
The Oracle Database enables the transparent access, processing, and integration of external flat files:
• Transparent access of external data from within the database
• Highly efficient procedural processing of complex data structures
• Optimal integration into SQL for bulk processing
17-15 Copyright © 2004, Oracle. All rights reserved.
External Tables and the Oracle Database
Oracle Database
10, 50 ml Beaker, .., 22.00, 5.0020, 50 ml Conical Flask, ... ,25.50, 8.5030, 50 ml Measuring Cylinder, ...,50.00, 9.0040, ...
Data in the external file
PRODUCTS_EXT
PROD_ID PROD_NAME ... PRICE DISCOUNT
10 50 ml Beaker 22.00 5.0020 50 ml Conical Flask 25.50 8.5030 50 ml Measuring Cylinder 50.00 9 .00...
CREATE TABLE PRODUCTS_EXT…;
SELECT .. FROM PRODUCTS_EXT;
17-16 Copyright © 2004, Oracle. All rights reserved.
External Tables: Example
CREATE OR REPLACE DIRECTORY trace_files AS '/u01/app/oracle/admin/orcl/bdump';
CREATE TABLE alert_log ( line_text VARCHAR2(80) )ORGANIZATION EXTERNAL( TYPE ORACLE_LOADER DEFAULT DIRECTORY trace_files ACCESS PARAMETERS ( RECORDS DELIMITED BY NEWLINE BADFILE 'rejects.bad' LOGFILE 'output.log' FIELDS (line_text CHAR(80)) ) LOCATION ('alert_orcl.log'))REJECT LIMIT UNLIMITED;
17-17 Copyright © 2004, Oracle. All rights reserved.
Retrieving User Context Attributes
Retrieve the following information:
• The name of the user who logged onto the database
• The method by which that user was authenticated
SQL> SELECT SYS_CONTEXT ('USERENV', 2> 'SESSION_USER') "USER",3> SYS_CONTEXT ('USERENV', 4> 'AUTHENTICATION_TYPE') Auth_meth
5> FROM DUAL;
USER AUTH_METH----------------- --------------------OE DATABASE
17-19 Copyright © 2004, Oracle. All rights reserved.
Resumable Statements
A resumable statement:
• Allows you to suspend large operations instead of receiving an error
• Gives you a chance to fix the problem while the operation is suspended, rather than starting over
• Is suspended for these conditions:– Out of space– Maximum extents reached– Space quota exceeded
17-20 Copyright © 2004, Oracle. All rights reserved.
Using Resumable Space Allocation
• Queries, DML operations, and certain DDL operations can be resumed if they encounter an out-of-space error.
• A resumable statement can be issued through SQL, PL/SQL, SQL*Loader, or the Oracle Call Interface (OCI).
• To run a resumable statement, you must first enable resumable statements for your session.
ALTER SESSION ENABLE RESUMABLE;
INSERT INTO sales_new SELECT * FROM sh.sales;
ALTER SESSION DISABLE RESUMABLE;
17-22 Copyright © 2004, Oracle. All rights reserved.
Resuming Suspended Statements
SQL Statement EMPLOYEES table
Suspended
EMPLOYEES table
Free space
Used block
Continue SQL operation
AFTER SUSPEND
triggerABORT
17-24 Copyright © 2004, Oracle. All rights reserved.
Summary
In this lesson, you should have learned how to:
• Generate scripts using SQL queries
• Use Dynamic SQL to automate script generation
• Create an external table
• Retrieve user context variables
• Issue statements that can be suspend upon encountering space condition errors
17-25 Copyright © 2004, Oracle. All rights reserved.
Practice 17 Overview: Using Dynamic SQL
This practice covers the following topics:
• Using Dynamic SQL
• Retrieving user context information