Pages

Thursday, September 11, 2014

sqlplus options

 Oracle Complete Reference for sqlplus options
=======================
command line options
=======================
sqlplus -?    - Help for command line options

sqlplus -v    - Return version for sqlplus and database
                                     for example:
                                     SQL*Plus: Release 19.0.0.0.0 - Production
                                     Version 19.3.0.0.0


sqlplus -S    - Silent mode. This would suppress  the SQL*Plus banner, prompts, 
                                   and echoing of commands.

sqlplus -L    - Attempts to log on just once.

sqlplus -prelim - Opens session in Preliminary mode.
                                       Sometimes, because the threashold of sessions is reached, 
                                       a DBA cannot login to the database. Even to perform a SHUTDOWN.
                                      In this case, the solution would be to use the -prelim option. 
                                      When opening a session in Preliminary mode, it will not try to create
                                      private session structures in the SGA. 
                                      This allows you to connect to perform debugging or shutdown
                                      operations. 


sqlplus -R <level>
                               - Restrict mode.
                                 This would disable SQL*Plus commands that interact with the file system.
                                 The level can be 1, 2 or 3.  
                                 The most restrictive is -R 3.
                                 R3  which disables all user commands interacting with the file system.

sqlplus -M "<options>"
                               - Markup mode. 
                                 Would generate output in HTML format.
                                 Sets automatic HTML markup of output.  
                                The options have the form:
                                 HTML [ON|OFF] 
                                 [HEAD text] 
                                 [BODY text] 
                                 [TABLE text]
                                 [ENTMAP {ON|OFF}] 
                                 [SPOOL {ON|OFF}] 
                                 [PRE[FORMAT] {ON|OFF}]

sqlplus /nolog 
                                Login without connecting to any schema.

=======================
Connect as sysdba
=======================
From privileged account:
sqlplus / as sysdba
or
sqlplus /nolog
connect as sysdba


In Oracle 9, one must use ""
sqlplus "/as sysdba"
=======================
Display options
=======================
SET COLSEP ,      - Separate columns with a comma
SET ESCAPE \      - This allows to enter special characters into database as strings. 
                                               For example 'A\&B' would be parsed to string 'A&B', rather than
                                               to variable substitution.
SET DEFINE OFF    - Do not substitute variables with using &&
SET ECHO OFF      - Do not print the sql command
SET FEEDBACK OFF  - Do not display "5 rows selected"
SET HEADING OFF   - Do not display Heading line
SET HEADSEP OFF   - This may or may not be useful...depends on your headings.
SET LINESIZE X    - X should be the sum of the column widths
SET LONG X        - X number of displayed characters for LONG column
SET LONGCHUNKSIZE X - Controls the number of characters retrieved at one time from a 
                                                  LONG column. 
                                                  The Default is 80. 
                                                  With this default, the one long string coming from CLOB - 
                                                  will be split into several 80 chars lines. each line is 
                                                  a separate iteration to sever.
                    LONGCHUNKSIZE value should be equal to LONG.

SET NEWPAGE NONE  - Get rid on first empty line in spool file.
SET NUMW X        - X should be the length you want for numbers.
SET PAGESIZE 0    - No header rows
SET SHOW OFF      - Do not display old: value new: value messages for SET changes.
SET SERVEROUTPUT ON-Enable messaged from DBMS_OUTPUT to be printed to console.
SET SQLBLANKLINES ON - Ignore blank lines. 
                                                This is useful when dealing with multi line commands, 
                                                 and not using '_' as "line continuation".
SET TERMOUT OFF   - Do not display output generated by commands in script.
SET TRIMSPOOL ON  - Trim the trailing blanks from file.
SET TRIMOUT ON    - Trim the trailing blanks on screen.
SET TIMING ON     - Display the elapsed time
SET VERIFY OFF    - Do not display old: value new: value messages 
                                               for variable substitution.
SET UNDERLINE OFF - Do not underline heading 

NLS
Format date with NLS
ALTER SESSION SET NLS_DATE_FORMAT='YYYY:MMDD HH24:MI:SS';

COLUMN MY_COLUMN FORMAT A300 WORD_WRAP
WORD_WRAP         - Wraps each line to the length specified by LINESIZE. 
                   That way words are not cut between lines.
NEW_VALUE         - An option to define, and assign value to the defined variable.
Usage of NEW_VALUE Example:

COL report_name_col NEW_VALUE report_name NOPRINT

SELECT 'MY_FILE_'||TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYYMM')||'.csv' report_name_col FROM DUAL;
SELECT &report_name FROM DUAL
SPOOL &report_name

=======================
Using variables
=======================
Option A. Static Variables
SET SHOW OFF
SET VERIFY OFF

DEFINE report_name = SELECT 'AIRCEL_HSC_REP_295_'||'201512'||'.csv' FROM DUAL;
SPOOL &report_name

Option B. Dynamic Variables
SET SHOW OFF
SET VERIFY OFF

COL report_name_col NEW_VALUE report_name NOPRINT
SELECT 'AIRCEL_HSC_REP_295_'||TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYYMM')||'.csv' 
SPOOL &report_name


=========================
How to get rig of trailing blanks in output file
=========================
SET TRIMSPOOL ON
SET TRIMOUT ON
SET LINESIZE 4000
This will set output line to 4000, and the trailing lines in generated file will be trimmed.


=======================
Format Display options
=======================
COL COL_A NOPRINT - Suppress display of a column

=======================
Additional options
=======================
exec DBMS_LOCK.SLEEP(120); - Sleep in sqlplus
REM - Remark line
=======================
HTML Display options
=======================
SET MARKUP HTML ON SPOOL ON  - Would produce output in HTML format.
                             i.e. HTML pages automatically encapsulated with
                             <HTML> and <BODY> tags.
PREFORMAT OFF 
ENTMAP ON 

=======================
spool options
=======================
SPOOL my_file
SPOOL my_file append


Error: SP2-0333: Illegal spool file name: (bad character: ' ')
SPOOL my_file append return an error.
Reason: append mode is available from sqlplus version 10 and higher. 
Example:

SQL> spool bbb append
SP2-0333: Illegal spool file name: "bbb append" (bad character: ' ')

SQL> select * from V$VERSION;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
PL/SQL Release 11.1.0.7.0 - Production
CORE    11.1.0.7.0      Production
TNS for 32-bit Windows: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
SQL> exit

>sqlplus -version
SQL*Plus: Release 9.2.0.1.0 - Production

As a workaround, use the OS append option
TYPE file2 >> file1
less file2 >> file1
=======================
Example, spool to a file without spool to sqlplus window.
=======================
Create new file my_sql.sql
SET FEEDBACK OFF
SET HEADING OFF 
SET NEWPAGE NONE
SET SHOW OFF  
SET VERIFY OFF
SET TERMOUT OFF
SET TRIMSPOOL ON
SET PAGESIZE 0
SET LINESIZE 600
SET COLSEP "," SPOOL my_output.txt SELECT col_1||' , '||col_2||' , '||col_3 FROM MY_TABLE;
SPOOL OFF
Run the sqlplus
sqlplus -s my_user/my_pass@orainst @my_sql.sql
=======================
IF in sqlplus
=======================
-- Activate Variables Substitution
SET DEFINE ON -- Suppress echo for substitution variables SET VERIFY OFF -- activate STDOUT SET SERVEROUTPUT ON
SET FEEDBACK OFF
COL INSTALL_AUTO_UPD NEW_VAL INSTALL_AUTO_UPD_VAL COL Auto_Activate_GSM_NET FOR A22 DEFINE Auto_Activate_GSM_NET=0
PROMPT Running with Auto_Activate_GSM_NET set to Value &&Auto_Activate_GSM_NET SET TERMOUT OFF
SELECT DECODE (&&Auto_Activate_GSM_NET,1,'cre_moco_util_objects.sql','dummy_install.sql')
AS INSTALL_AUTO_UPD
FROM DUAL;
SET TERMOUT ON
PROMPT PROMPT ======================================= PROMPT CREATE OR REPLACE PACKAGE MOCO_UTIL PROMPT ======================================= @@ &INSTALL_AUTO_UPD_VAL PROMPT PROMPT ======================================= PROMPT Done PROMPT ======================================= PROMPT SET DEFINE OFF SPOOL OFF

=======================
Example, split a file to 10 files, and append commit at the end
=======================
Example: Split input file aa.sql to several files, each one 3000 Lines. And append commit at the end of each one

split -l 3000 -e --additional-suffix=.sql aa.sql find . -type f -print0 | while IFS= read -r -d $'\0' file; do echo "$file" | echo "commit;" >> "$file" ; done
=======================
Reference
=======================
SQL*Plus Command Reference in docs.oracle.com
Generating HTML Reports from SQL*Plus Oracle Reference

Nice Blog: Simulating Control Flow in sqlplus

Tuesday, September 9, 2014

Materialized View By Example - Changing the Base Table.

General
Consider the following scenario:
There is a scheduled FAST refresh from a base table.
Now there are new column in the Base Table.
How to handle the refresh procedure so it will handle the change?


Example
For Example, there is a table GATES in schema A with a Materialized View GATES_MV in schema B, on another Instance(s).
Schema A.GATES structure changes.

Steps to handle this change:
On Schema A
1. Drop existing
MLOG$_GATES table
   All MLOG$ tables are listed in USER_MVIEW_LOGS

   This is done by: DROP MATERIALIZED VIEW LOG ON GATES.


2. Re-Create MLOG$_GATES table.
   This is done by: CREATE MATERIALIZED VIEW LOG ON GATES.

   Check that MLOG$_GATES is now created.


SQL> DESC MLOG$_GATES
Name            Type        Nullable Default Comments 
--------------- ----------- -------- ------- -------- 
GATE_ID         NUMBER(10)  Y                         
SNAPTIME$$      DATE        Y                         
DMLTYPE$$       VARCHAR2(1) Y                         
OLD_NEW$$       VARCHAR2(1) Y                         

CHANGE_VECTOR$$ RAW(255)    Y 


SQL> DESC GATES;
Name                  Type         Nullable Default Comments 
--------------------- ------------ -------- ------- -------- 
GATE_ID               NUMBER(10)                             
NETWORK_ID            NUMBER(10)                             
PREFERRED_IND         NUMBER(1)              

PREFERRED_IND is the new column.

3. Run Refresh on the Remotes DB, i.e. in Schema B.

Step A
Run the COMPLETE Refresh - to validate there are no issues with the refresh.



EXEC DBMS_SNAPSHOT.REFRESH('GATES_MV','C');

Step B
Assuming there is a scheduled FAST refresh, run the FAST Refresh - To set the baseline for next scheduled refresh.
EXEC DBMS_SNAPSHOT.REFRESH('GATES_MV','F');

SQL> DESC GATES_MV;
Name                  Type         Nullable Default Comments 
--------------------- ------------ -------- ------- -------- 
GATE_ID               NUMBER(10)                             
NETWORK_ID            NUMBER(10)                             
PREFERRED_IND         NUMBER(1)             


Appendix
Nice Blog with Examples:  Materialized Views

Monday, September 1, 2014

Users and Profiles in Oracle

=================================
General
=================================
Profiles are used to managed advanced settings for Oracle users.

Flow of Events
1. Create a Profile
2. Edit profile properties
3. Assign profile to user.

=================================
View Current Settings
=================================
To see current user profile:
SELECT username, profile FROM DBA_USERS;

DESCRIBE DBA_PROFILES 

Name          Null?    Type
------------- -------- -------------
PROFILE       NOT NULL VARCHAR2(30)
RESOURCE_NAME NOT NULL VARCHAR2(32)
RESOURCE_TYPE          VARCHAR2(8)
LIMIT                  VARCHAR2(40)


SELECT resource_name, resource_type, limit FROM DBA_PROFILES
WHERE profile = 'DEFAULT'


RESOURCE_NAME                RESOURCE_TYPE  LIMIT
---------------------------  -------------  --------------
COMPOSITE_LIMIT              KERNEL         UNLIMITED
CONNECT_TIME                 KERNEL         UNLIMITED
CPU_PER_CALL                 KERNEL         UNLIMITED
CPU_PER_SESSION              KERNEL         UNLIMITED
IDLE_TIME                    KERNEL         UNLIMITED
LOGICAL_READS_PER_CALL       KERNEL         UNLIMITED
LOGICAL_READS_PER_SESSION    KERNEL         UNLIMITED
PRIVATE_SGA                  KERNEL         UNLIMITED
SESSIONS_PER_USER            KERNEL         UNLIMITED

FAILED_LOGIN_ATTEMPTS        PASSWORD       10
PASSWORD_GRACE_TIME          PASSWORD       7
PASSWORD_LIFE_TIME           PASSWORD       UNLIMITED
PASSWORD_LOCK_TIME           PASSWORD       1
PASSWORD_REUSE_MAX           PASSWORD       UNLIMITED
PASSWORD_REUSE_TIME          PASSWORD       UNLIMITED
PASSWORD_VERIFY_FUNCTION     PASSWORD       NULL

=================================
Profile parameters
=================================
There are two types of profile parameters:
- KERNEL
- PASSWORD

=================================
KERNEL Parameters
=================================
- COMPOSITE_LIMIT
Total resource cost for a session, in service units.Oracle Database calculates the total service units as a weighted sum of CPU_PER_SESSION, CONNECT_TIME, LOGICAL_READS_PER_SESSION, and PRIVATE_SGA.

- CONNECT_TIME

Total elapsed time limit for a session, in minutes.

- CPU_PER_CALL
CPU time limit for a call (a parse, execute, or fetch) in hundredths of seconds.

- CPU_PER_SESSION
CPU time limit for a session in hundredth of seconds.

- IDLE_TIME
Permitted periods of continuous inactive time during a session, in minutes. Long-running queries and other operations are not subject to this limit.

- LOGICAL_READS_PER_CALL
Number of data blocks read for a call to process a SQL statement (a parse, execute, or fetch).

- LOGICAL_READS_PER_SESSION
Number of data blocks read in a session, including reads from memory and disk.

- PRIVATE_SGA
Amount of private space a session can allocate in the shared pool of SGA in bytes.

- SESSIONS_PER_USER
Number of concurrent sessions per user.

=================================
Default Profile

=================================
If a value is set to DEFAULT, the actual value is taken from the DEFAULT profile. 
The DEFAULT profile initially has all limits set to UNLIMITED.


=================================
Creating a Profile
=================================
Any user who is not explicitly assigned a profile is subject to the limits defined in the DEFAULT profile. 
Also, if the profile that is explicitly assigned to a user omits limits for some resources or specifies DEFAULT for some limits, then the user is subject to the limits on those resources defined by the DEFAULT profile.


Example of creating a Profile

Step A. - Create a Profile
CREATE PROFILE my_profile 
LIMIT  PASSWORD_LIFE_TIME       UNLIMITED
       PASSWORD_REUSE_TIME      UNLIMITED 
       PASSWORD_REUSE_MAX       UNLIMITED
       PASSWORD_VERIFY_FUNCTION NULL
       PASSWORD_LOCK_TIME       UNLIMITED
       PASSWORD_GRACE_TIME      UNLIMITED ;

Step B. - Assign the Profile to user.

ALTER USER my_user PROFILE my_profile;

See the current settings:

SELECT username, profile FROM DBA_USERS WHERE username='MY_USER';

SELECT * FROM DBA_PROFILES WHERE profile='MY_PROFILE';


Oracle Profile and Password Management
Password management in Oracle is done via Profile assigned to a user.


This is done with two parameters:

PASSWORD_REUSE_TIME 
PASSWORD_REUSE_MAX
These parameters can have an Integer value or UNLIMITED

Option A.

If parameters are set to UNLIMITED, passwords can be reused freely. 

Option B.

If one of these limits is set to UNLIMITED, and the other is set to an integer, the user cannot reuse passwords. 

Option C.

If both limits are set to integers, then a password can be reused after the PASSWORD_REUSE_TIME number of days has expired and the password has been changed less than PASSWORD_REUSE_MAX number of times.


How to handle Oracle Error: "ORA-28007 The password cannot be reused" without changing password by example.

SQL> ALTER USER SOME_USER IDENTIFIED BY SOME_PASSWORD;
ALTER USER SOME_USER IDENTIFIED BY SOME_PASSWORD
*
ERROR at line 1:
ORA-28007: the password cannot be reused

SQL> SELECT * FROM DBA_PROFILES WHERE profile='DEFAULT' AND RESOURCE_TYPE like '%PASSWORD%';


PROFILE             RESOURCE_NAME                    RESOURCE LIMIT

------------------- -------------------------------- -------- ----------------
DEFAULT             FAILED_LOGIN_ATTEMPTS            PASSWORD 3
DEFAULT             PASSWORD_LIFE_TIME               PASSWORD 60
DEFAULT             PASSWORD_REUSE_TIME              PASSWORD 15
DEFAULT             PASSWORD_REUSE_MAX               PASSWORD 3
DEFAULT             PASSWORD_VERIFY_FUNCTION         PASSWORD VERIFY_FUNCTION
DEFAULT             PASSWORD_LOCK_TIME               PASSWORD UNLIMITED
DEFAULT             PASSWORD_GRACE_TIME              PASSWORD 5


SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_REUSE_TIME UNLIMITED;

Profile altered.
SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_REUSE_MAX UNLIMITED;
Profile altered.
SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME 365;
Profile altered.

SQL> ALTER USER SOME_USER IDENTIFIED BY SOME_PASSWORD;

User altered.

SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_REUSE_TIME 15;

Profile altered.
SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_REUSE_MAX 3;
Profile altered.
SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME 60;
Profile altered.

SELECT USERNAME, ACCOUNT_STATUS, EXPIRY_DATE 

  FROM DBA_USERS 
 WHERE username = 'SOME_USER';

USERNAME            ACCOUNT_STATUS                   EXPIRY_DA

------------------- -------------------------------- ---------
SOME_USER           OPEN                             14-AUG-15


Appendix
Short Reference: Orafaq




Thursday, August 28, 2014

Batch script by example. Run batch, that call sqlplus that generate csv files, batch file merge these files into one.

The Task
Need to generate csv file, which holds data collected from several schemas in same Instance.
The output file should hold data about services and ports used by applications. Each schema represents a different application.


The Flow
In order to query several schemas, the script creates new user, "query_user".
This user is granted 
SELECT ANY TABLE and CREATE VIEW permissions.
For each schema, a new view is created, queried, and then dropped.

The output of the query, is saved to csv file.

The flow in general:
Run main batch file.
Batch file calls main sqlplus script.
Open loop on schemas.

 - The sqlplus script builds a series of sqlplus scripts, each one per specific task.
 - The sqlplus script then executes the generated scripts.
 - The data is retrieved via view owned by "query_user" on another schema at a time.
 - Each generated script generates its own csv file.
Close loop on schemas.
Main sqlplus script terminates.
Batch file calls another batch script, that merges the generated csv files into one file.
Cleanup script, drop query_user, drop view, etc.

The files:
bat files
main_get_ports.bat
main_retrieve_data.bat
merge_output_files.bat
cleanup_output_files.bat

sqlplus files
main_get_ports.sql
set_connection.sql


create_query_user.sql

build_grant_permissions.sql
grant_permissions_user.sql
grant_permissions.sql

build_create_view_and_select.sql
create_view_and_select.sql
create_view.sql

select_header_procs.sql
select_header_params.sql
select_from_view_procs.sql
select_from_view_params.sql

drop_view.sql

===========================
main_get_ports.bat
===========================
ECHO OFF
SET HOME_PATH=L:\Functionality\PORTS
SET INPUT_FOLDER=L:\Functionality\PORTS\scripts
SET OUTPUT_FOLDER=L:\Functionality\PORTS
SET OUTPUT_FILE_NAME=ports_list.csv

ECHO.
ECHO -------------------------------------
ECHO Starting Running Script
ECHO -------------------------------------
ECHO.
ECHO Generating script: %OUTPUT_FOLDER%\%OUTPUT_FILE_NAME%
ECHO.

cd %HOME_PATH%
cd scripts
call main_retrieve_data.bat
cd %HOME_PATH%
call scripts\merge_output_files.bat %HOME_PATH% %INPUT_FOLDER% %OUTPUT_FOLDER% %OUTPUT_FILE_NAME%
call scripts\cleanup_output_files.bat %HOME_PATH%\scripts

ECHO -------------------------------------
ECHO Completed Successfully
ECHO -------------------------------------


===========================
main_retrieve_data.bat
===========================
sqlplus -s /nolog @main_get_ports.sql

===========================
main_get_ports.sql
===========================
@set_connection.sql
--SET TERMOUT OFF

Conn &&query_user/&&query_pass@&&query_connectstr

SET TERMOUT ON
SET SHOW OFF
SET VERIFY OFF 
SET HEAD OFF
SET LINE 500
SET FEEDBACK OFF
SET PAGES 0
SET TRIMSPOOL ON


PROMPT -----------------------;
PROMPT Grant Permissions Start
@build_grant_permissions.sql
@grant_permissions.sql
SET TERMOUT ON
PROMPT Grant Permissions Completed
PROMPT -----------------------;
PROMPT

PROMPT
PROMPT -----------------------;
PROMPT build_create_view_and_select.sql Start
@build_create_view_and_select.sql
SET TERMOUT ON
PROMPT build_create_view_and_select.sql Completed
PROMPT -----------------------;
PROMPT


PROMPT
PROMPT -----------------------;
PROMPT create_view_and_select.sql Start
@create_view_and_select.sql
SET TERMOUT ON
PROMPT create_view_and_select.sql Completed
PROMPT -----------------------;
PROMPT
EXIT;
/



===========================
create_query_user.sql
===========================
------------------------------------------------
--Login as system/sysdba to run this script
------------------------------------------------
CREATE USER query_user IDENTIFIED BY query_user;
GRANT CONNECT TO query_user;
GRANT CREATE VIEW TO query_user;
GRANT SELECT ANY TABLE TO query_user;

EXIT;


===========================
set_connection.sql
===========================
DEFINE query_user=query_user
DEFINE query_pass=query_user
DEFINE query_connectstr=inst_test
DEFINE main_user=query_user 




===========================
build_grant_permissions.sql
===========================
@set_connection.sql
SET TERMOUT OFF

Conn &&query_user/&&query_pass@&&query_connectstr
SET SHOW OFF
SET VERIFY OFF
SET HEAD OFF
SET LINE 500
SET FEEDBACK OFF
SET PAGES 0
SET TRIMS ON

-- This goes into grant_permissions.sql
SPOOL grant_permissions.sql
PROMPT -------------------------------;
PROMPT SET TERMOUT OFF
PROMPT SET SHOW OFF
PROMPT SET HEAD ON
PROMPT SET FEEDBACK OFF
PROMPT SET VERIFY OFF

--PROMPT DEFINE main_user='';
--PROMPT COLUMN main_user new_value main_user for a30;
--PROMPT SELECT SYS_CONTEXT('USERENV','SESSION_USER') main_user FROM DUAL;
--PROMPT SELECT '&&main_user' from dual;
PROMPT -------------------------------;
PROMPT

SELECT '@grant_permissions_user.sql'||' '||owner||' '||owner||' '||'&&query_connectstr' || ' '||'&&main_user' FROM ALL_TABLES WHERE TABLE_NAME = 'PROCESS';
SPOOL OFF
/


===========================
grant_permissions.sql
===========================
This file is generated by build_grant_permissions.sql
It looks like this:

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

SET TERMOUT OFF
SET SHOW OFF
SET HEAD ON
SET FEEDBACK OFF
SET VERIFY OFF
-------------------------------

@grant_permissions_user.sql SCHEMA_A_USER SCHEMA_A_PASS CONNECT_STR query_user
@grant_permissions_user.sql SCHEMA_B_USER SCHEMA_B_PASS CONNECT_STR query_user


===========================
grant_permissions_user.sql
===========================
SET SHOW OFF
SET VERIFY OFF
SET HEAD OFF
SET FEEDBACK OFF
SET PAGES 0
SET TRIMS ON

DEFINE user_name=&&1
DEFINE user_pass=&&2
DEFINE conn_str=&&3
DEFINE main_user=&&4

conn &&user_name/&&user_pass@&&conn_str

GRANT SELECT ON PRODUCT TO &&main_user;
GRANT SELECT ON CONFIGURATION_PROPERTIES TO &&main_user;
GRANT SELECT ON PROCESS TO &&main_user;
/


===========================
build_create_view_and_select.sql
===========================
@set_connection.sql
--SET TERMOUT OFF

Conn &&query_user/&&query_pass@&&query_connectstr

SET TERMOUT ON
SET SHOW OFF
SET VERIFY OFF
SET HEAD OFF
SET LINE 500
SET FEEDBACK OFF
SET PAGES 0
SET TRIMSPOOL ON

SPOOL create_view_and_select.sql
PROMPT -------------------------------;
--PROMPT PROMPT inside SPOOL create_view_and_select.sql
SET TERMOUT OFF
SET SHOW OFF
SET VERIFY OFF
SET HEADING ON
SET LINE 500
SET FEEDBACK OFF
SET PAGES 0
SET TRIMSPOOL ON

PROMPT -------------------------------;
PROMPT

--PROMPT SPOOL ports_list.csv
--SELECT '@create_view.sql'||' '||owner||';'||CHR(10)||'@select_from_view.sql'||' '||'ports_list_'||owner||'.csv'||';' ||CHR(10)||'SET HEADING OFF'||CHR(10) FROM ALL_TABLES WHERE TABLE_NAME = 'PROCESS';

SELECT '@create_view.sql'||' '||owner||';'FROM ALL_TABLES WHERE TABLE_NAME = 'PROCESS';
PROMPT SET HEADING ON
SELECT '@select_header_procs.sql'||' '||'ports_proc_header.csv'||';' FROM ALL_TABLES WHERE ROWNUM < 2;
SELECT '@select_header_params.sql'||' '||'ports_params_header.csv'||';' FROM ALL_TABLES WHERE ROWNUM < 2;
PROMPT SET HEADING OFF
SELECT '@create_view.sql'||' '||owner||';'||CHR(10)||'@select_from_view_procs.sql'||' '||'ports_proc_list_'||owner||'.csv'||';' FROM ALL_TABLES WHERE TABLE_NAME = 'PROCESS';
SELECT '@create_view.sql'||' '||owner||';'||CHR(10)||'@select_from_view_params.sql'||' '||'ports_params_list_'||owner||'.csv'||';' FROM ALL_TABLES WHERE TABLE_NAME = 'PROCESS';
SELECT '@drop_view.sql'||';' FROM DUAL;
--PROMPT SPOOL OFF
PROMPT /
SPOOL OFF
/


===========================
create_view_and_select.sql
===========================
This file is generated by build_create_view_and_select.sql
It looks like this:

@create_view.sql SCHEMA_A_USER;
@create_view.sql SCHEMA_B_USER;
SET HEADING ON
@select_header_procs.sql ports_proc_header.csv;
@select_header_params.sql ports_params_header.csv;
SET HEADING OFF
@create_view.sql SCHEMA_A_USER;
@select_from_view_procs.sql ports_proc_list_RUS_TEST1_SPARX.csv;

@create_view.sql SCHEMA_B_USER;
@select_from_view_procs.sql ports_proc_list_RUS_TEST2_SPARX.csv;

@create_view.sql SCHEMA_A_USER;
@select_from_view_params.sql ports_params_list_SCHEMA_A_USER.csv;

@create_view.sql SCHEMA_B_USER;
@select_from_view_params.sql ports_params_list_SCHEMA_B_USER.csv;

@drop_view.sql;
/

===========================
Handle Logic files
===========================
Following files handle the logic.
They are not important for a general example.
create_view.sql
select_header_procs.sql
select_header_params.sql

===========================
create_view.sql
===========================
DEFINE my_user=&&1;

--SET HEAD ON
--PROMPT --------------------------------------------;
--PROMPT Running create_view.sql for user '&&my_user'
--PROMPT --------------------------------------------;

CREATE OR REPLACE VIEW PROCESS_PORTS_VW AS
SELECT RTRIM('&&my_user') AS schema_name,
       RTRIM('PRODUCTS CONFIGURATION') AS source,
       item_id AS service_name,
       PRODUCTS.jvm_id AS jvm_id,
       PRODUCTS.jmx_port AS jmx_port,      
       CONFIGURATION.property_value AS property_value,
       CONFIGURATION.property_description AS property_description,
       NULL AS process_name,
       NULL AS process_type,      
       NULL AS process_port,
       NULL AS server_port
      FROM &&my_user..PRODUCTS PRODUCTS,
           &&my_user..CONFIGURATION_PROPERTIES CONFIGURATION    
 WHERE PRODUCTS.product_id = CONFIGURATION.property_id
   AND 1=1
UNION ALL
SELECT '&&my_user' AS schema_name,
       'PROCESS' AS source,
       NULL AS service_name,
       NULL AS jvm_id,      
       NULL AS jmx_port,
       NULL AS property_value,
       NULL AS property_description,
       process_name,
       process_type,
       process_port,
       server_port      
FROM   &&my_user..PROCESS
 WHERE 1=1
ORDER BY process_name   ;

--PROMPT --------------------------------------------;
--PROMPT create_view.sql Completed
--PROMPT --------------------------------------------;



===========================
select_header_procs.sql
===========================

DEFINE my_file=&&1;

SET COLSEP ,    
SET TERMOUT OFF
SET SHOW OFF
SET VERIFY OFF
SET LINESIZE 1000
SET FEEDBACK OFF
SET PAGES 10
SET TRIMSPOOL ON
SET UNDERLINE OFF
SET HEADING ON
SET NEWPAGE NONE

COL property_value FOR A50
COL service_name FOR A12
COL jvm_id FOR 999999
COL property_description FOR A150

SPOOL &&my_file
SELECT schema_name,
       source,
  process_name,
  process_type,
  process_port,
  server_port  
FROM PROCESS_PORTS_VW
WHERE source = 'PROCESS'
  AND ROWNUM < 2;

SPOOL OFF;
===========================
select_header_params.sql
===========================

DEFINE my_file=&&1;

SET COLSEP ,    
SET TERMOUT OFF
SET SHOW OFF
SET VERIFY OFF
SET LINESIZE 1000
SET FEEDBACK OFF
SET PAGES 10
SET TRIMSPOOL ON
SET UNDERLINE OFF
SET HEADING ON
SET NEWPAGE NONE

COL property_value FOR A50
COL service_name FOR A12
COL jvm_id FOR 999999
COL property_description FOR A150

SPOOL &&my_file
SELECT schema_name,
       source,
       service_name,
       jvm_id,
       jmx_port,
       property_value,
       property_description
FROM PROCESS_PORTS_VW
WHERE source = 'PRODUCTS CONFIGURATION'
  AND ROWNUM<2;
SPOOL OFF;


===========================
Generate csv file
===========================
Following file use the view, to retrieve data, and create csv files.
select_from_view_procs.sql
select_from_view_params.sql


===========================
select_from_view_procs.sql
===========================

DEFINE my_file=&&1;

SET COLSEP ,     
SET TERMOUT OFF
SET SHOW OFF
SET VERIFY OFF 
SET LINESIZE 1000
SET FEEDBACK OFF
SET PAGESIZE 0
SET TRIMSPOOL ON
SET UNDERLINE OFF
SET HEADING OFF

COL property_value FOR A50
COL service_name FOR A12
COL jvm_id FOR 999999
COL property_description FOR A150

SPOOL &&my_file
SELECT schema_name,
       source,
  process_name,
  process_type,
  process_port,
  server_port   
FROM PROCESS_PORTS_VW
WHERE source = 'PROCESS';

SPOOL OFF;
===========================
select_from_view_params.sql
===========================

DEFINE my_file=&&1;

SET COLSEP ,     
SET TERMOUT OFF
SET SHOW OFF
SET VERIFY OFF 
SET LINESIZE 1000
SET FEEDBACK OFF
SET PAGES 0
SET TRIMSPOOL ON
SET UNDERLINE OFF
SET HEADING OFF

COL property_value FOR A50
COL service_name FOR A12
COL jvm_id FOR 999999
COL property_description FOR A150

SPOOL &&my_file
SELECT schema_name,
       source,
  service_name,
  jvm_id,
  jmx_port,
  property_value,
  property_description
FROM PROCESS_PORTS_VW
WHERE source = 'PRODUCTS CONFIGURATION';  
SPOOL OFF;

===========================
drop_view.sql
===========================
--PROMPT --------------------------------------------;
--PROMPT Drop PROCESS_PORTS_VW
--PROMPT --------------------------------------------;

DROP VIEW PROCESS_PORTS_VW;

--PROMPT --------------------------------------------;
--PROMPT Drop PROCESS_PORTS_VW Completed
--PROMPT --------------------------------------------;


===========================
merge_output_files.bat
===========================
ECHO OFF
SET HOME_PATH=%1%
SET INPUT_FOLDER=%2%
SET OUTPUT_FOLDER=%3%
SET OUTPUT_FILE_NAME=%4%


REM ---------------------------------------------------------
REM This program would merge all files into one file.
REM Target File: OUTPUT_FILE_NAME
REM Source files: 
REM Header Files:  ports_params_header.csv
REM                ports_proc_header.csv
REM Data Files:    ports_params_list_*.csv
REM                ports_proc_list_*.csv
REM ---------------------------------------------------------


TYPE %INPUT_FOLDER%\ports_params_header.csv | find "SCHEMA_NAME" > %OUTPUT_FOLDER%\%OUTPUT_FILE_NAME%
FOR %%i IN (%INPUT_FOLDER%\ports_params_list_*.csv) DO TYPE %%i >> %OUTPUT_FOLDER%\%OUTPUT_FILE_NAME%

ECHO. >> %OUTPUT_FOLDER%\%OUTPUT_FILE_NAME%

TYPE %INPUT_FOLDER%\ports_proc_header.csv | find "SCHEMA_NAME" >> %OUTPUT_FOLDER%\%OUTPUT_FILE_NAME%
FOR %%i IN (%INPUT_FOLDER%\ports_proc_list_*.csv) DO TYPE %%i  >> %OUTPUT_FOLDER%\%OUTPUT_FILE_NAME%


===========================
cleanup_output_files.bat
===========================
ECHO OFF
SET INPUT_FOLDER=%1%

REM ---------------------------------------------------------
REM This program would delete all temp files.
REM ---------------------------------------------------------

FOR %%i IN (%INPUT_FOLDER%\ports_params*.csv) DO DEL %%i

FOR %%i IN (%INPUT_FOLDER%\ports_proc*.csv) DO DEL %%i