Showing posts with label sqlplus. Show all posts
Showing posts with label sqlplus. Show all posts

Saturday, July 28, 2012

How to print date on sqlplus report title

COLUMN report_date_col NEW_VALUE report_date
col report_date_col noprint
SELECT TO_CHAR ( SYSDATE ,'DD-Mon-YYYY:HH:MI') AS report_date_col FROM dual;

TTitle left "*** Rollback Information ( Date: &report_date ) ***" skip 2

Tuesday, June 12, 2012

How to get my Oracle session informations

In Oracle/PLSQL, the userenv function can be used to retrieve information about the current Oracle session.

The syntax for the userenv function is:

userenv( parameter )

parameter is the value to return from the current Oracle session.

For Example:

userenv('ENTRYID') would return FALSE
userenv('LANGUAGE') would return 'AMERICAN_AMERICA.WE8DEC'

 The possible values for parameter are:

Parameter Explanation
CLIENT_INFO Returns user session information stored using the DBMS_APPLICATION_INFO package
ENTRYID Available auditing entry identifier
INSTANCE The identifier number of the current instance
ISDBA Returns TRUE if the user has DBA privileges. Otherwise, it will return FALSE.
LANG The ISO abbreviation for the language
LANGUAGE The language, territory, and character of the session. In the following format:
     language_territory.characterset
SESSIONID The identifier of the auditing session
TERMINAL The OS identifier of the current session

Monday, June 11, 2012

script to identify all objects belonging to a tablespace in Oracle

select owner
,      segment_name
,      segment_type
from   dba_segments
where  lower(tablespace_name) like lower('%&tablespace%')
order by owner, segment_name

Saturday, May 19, 2012

Setting up Autotrace in SQL*Plus

AUTOTRACE is a facility within SQL*Plus to show us the explain plan of the queries we've executed, and
the resources they used. There is more than one way to get AUTOTRACE configured.

This is how i do to get AUTOTRACE working:
•  cd $ORACLE_HOME/rdbms/admin (or cd %ORACLE_HOME%/rdbms/admin on Windows)
•  log into SQL*Plus as SYSTEM ==> sqlplus system/pass@alias
•  run @utlxplan
•  run CREATE PUBLIC SYNONYM PLAN_TABLE FOR PLAN_TABLE;
•  run GRANT ALL ON PLAN_TABLE TO PUBLIC;
You can replace the GRANT TO PUBLIC with some user if you want. By making it public, you let
anyone trace using SQL*Plus. This prevents every user from having to install their own plan table. The alternative is for you to run @utlxplan in every schema from which you
want to use AUTOTRACE.
The next step is creating and granting the PLUSTRACE role:
•  cd $ORACLE_HOME/sqlplus/admin (or cd %ORACLE_HOME%/sqlplus/admin on Windows)
•  log into SQL*Plus as SYS or AS SYSDBA
•  run @plustrce
•  run GRANT PLUSTRACE TO PUBLIC;
Again, you can replace PUBLIC in the GRANT command with some user if you want.

Wednesday, May 16, 2012

This is how i customize my sqlplus prompt

I often use sqlplus to perform my daily administrative tasks and i work on multiple servers that contain multiple databases, each database contains multiple accounts.
When connected to a database, i need to know my username and my database connection to reassure me that I point to the correct database in order to avoid errors that could be catastrophic.

To customize your sqlplus prompt
  • Navigate to the directory $ORACLE_HOME/sqlplus/admin
  • Create a file login.sql (or edit the file if it already exists) using vi on unix / Linux or notepad on windows
  • SET SQLPROMPT '&_USER.@&_CONNECT_IDENTIFIER.> '
      
     The next time you will login your sqlplus prompt will look like this : user@alias

Here is a complete list of SQL*Plus variables that you can use to customize your prompt.

There are eight variables defined during SQL*Plus installation. These variables only differ from user defined variables by having predefined values.
Table 12-3 Variables Predefined at SQL*Plus Installation
Variable Name Contains
_CONNECT_IDENTIFIER Connection identifier used to make connection, where available.
_DATE Current date, or a user defined fixed string.
_EDITOR Specifies the editor used by the EDIT command.
_O_VERSION Current version of the installed Oracle Database.
_O_RELEASE Full release number of the installed Oracle Database.
_PRIVILEGE Privilege level of the current connection.
_SQLPLUS_RELEASE Full release number of installed SQL*Plus component.
_USER User name used to make connection.

Tuesday, May 15, 2012

Establish a direct connection to Oracle from sqlplus (without an alias)

People like us who change environments all the time with various customers, not to waste time configuring a connection alias in the tnsnames.ora file you can directly connect to an ORACLE database from sqlplus using


sqlplus user/pass@server_ip:port/sid

ex: sqlplus guy/fomi@10.10.10.10:1521/test

Monday, May 14, 2012

SQLPLUS SET System Variable Summary

I need to lookup these variables very often for my sqlplus scripts

System Variable Description
Sets automatic registering of scripts through the DBMS_APPLICATION_INFO package.
Sets the number of rows, called a batch, that SQL*Plus will fetch from the database at one time.
Controls when Oracle Database commits pending changes to the database.
Sets the automatic printing of bind variables.
ON sets the RECOVER command to automatically apply the default filenames of archived redo log files needed during recovery.
Displays a report on the execution of successful SQL DML statements (SELECT, INSERT, UPDATE or DELETE).
Sets the non-alphanumeric character used to end PL/SQL blocks to c.
Sets the non-alphanumeric character used to separate multiple SQL*Plus commands entered on one line to c.
In iSQL*Plus, SET COLSEP determines the column separator character to be printed between column output that is rendered inside <PRE> tags. Sets the text to be printed between selected columns.
Sets the character you can use to terminate a substitution variable reference if you wish to immediately follow the variable with a character that SQL*Plus would otherwise interpret as a part of the substitution variable name.
Controls the number of batches after which the COPY command commits changes to the database.
Sets the suppression of the comparison of datatypes while inserting or appending to tables with the COPY command.
Sets the character used to prefix variables to c.
Sets the depth of the level to which you can recursively describe an object.
Controls whether the START command lists each command in a script as the command is executed.
Sets the default filename for the EDIT command.
Controls where on a page each report begins.
Defines the character you enter as the escape character.
Displays the number of records returned by a query when a query selects at least n records.
Checks to make sure that SQL statements conform to the ANSI/ISO SQL92 standard.
Controls when output is sent to the user's display device.
Controls printing of column headings in reports.
Defines the character you enter as the heading separator character.
Changes the default instance for your session to the specified instance path.
Sets the total number of characters that SQL*Plus displays on one line before beginning a new line.
Sets the starting position from which CLOB and NCLOB data is retrieved and displayed.
Specifies the location from which archive logs are retrieved during recovery.
Sets maximum width (in bytes) for displaying LONG, CLOB, NCLOB and XMLType values; and for copying LONG values.
Sets the size (in bytes) of the increments in which SQL*Plus retrieves a LONG, CLOB, NCLOB or XMLType value.
Outputs HTML marked up text, which is the output used by iSQL*Plus.
Sets the number of blank lines to be printed from the top of each page to the top title.
Sets the text that represents a null value in the result of a SQL SELECT command.
Sets the default format for displaying numbers.
Sets the default width for displaying numbers.
Sets the number of lines in each page.
Enables you to control scrolling of your terminal when running reports.
RECSEP tells SQL*Plus where to make the record separation.
Display or print record separators.
Controls whether to display the output (that is, DBMS_OUTPUT PUT_LINE) of stored procedures or PL/SQL blocks in SQL*Plus.
Enables correct alignment for terminals that display shift characters.
Controls whether SQL*Plus lists the old and new settings of a SQL*Plus system variable when you change the setting with SET.
Controls whether SQL*Plus puts blank lines within a SQL command or script.
Converts the case of SQL commands and PL/SQL blocks just prior to execution.
Sets the character sequence SQL*Plus displays as a prompt after you continue a SQL*Plus command on an additional line using a hyphen (–).
Sets the prompt for the second and subsequent lines of a SQL command or PL/SQL block.
Sets the behavior or output format of VARIABLE to that of the release or version specified by x.y[.z].
Sets the SQL*Plus prefix character.
Sets the SQL*Plus command prompt.
Sets the character used to end and execute SQL commands to c.
Sets the default file that SQL*Plus uses in commands that refer to scripts.
Determines how SQL*Plus formats white space in terminal output.
Controls the display of output generated by commands executed from a script.
Controls the display of the current time.
Controls the display of timing statistics.
Determines whether SQL*Plus puts trailing blanks at the end of each displayed line.
Determines whether SQL*Plus puts trailing blanks at the end of each spooled line.
Sets the character used to underline column headings in SQL*Plus reports to c.
Controls whether SQL*Plus lists the text of a SQL statement or PL/SQL command before and after SQL*Plus replaces substitution variables with values.
Controls whether SQL*Plus truncates the display of a SELECTed row if it is too long for the current line width.
Defines the base URI to use. This is useful to change the prefix of the file to access when writing generic XQuery expressions.
Controls the ordering of results from an XQuery.
Sets the preservation mode for notes created or returned.
Specifies an XQuery context item which can be either a node or a value.