
- •Contents
- •Send Us Your Comments
- •Preface
- •What’s New in SQL Reference?
- •1 Introduction to Oracle SQL
- •History of SQL
- •SQL Standards
- •Embedded SQL
- •Lexical Conventions
- •Tools Support
- •2 Basic Elements of Oracle SQL
- •Datatypes
- •Oracle Built-in Datatypes
- •ANSI, DB2, and SQL/DS Datatypes
- •Oracle-Supplied Types
- •"Any" Types
- •XML Types
- •Spatial Type
- •Media Types
- •Datatype Comparison Rules
- •Data Conversion
- •Literals
- •Text Literals
- •Integer Literals
- •Number Literals
- •Interval Literals
- •Format Models
- •Number Format Models
- •Date Format Models
- •String-to-Date Conversion Rules
- •XML Format Model
- •Nulls
- •Nulls in SQL Functions
- •Nulls with Comparison Conditions
- •Nulls in Conditions
- •Pseudocolumns
- •CURRVAL and NEXTVAL
- •LEVEL
- •ROWID
- •ROWNUM
- •XMLDATA
- •Comments
- •Comments Within SQL Statements
- •Comments on Schema Objects
- •Hints
- •Database Objects
- •Schema Objects
- •Nonschema Objects
- •Parts of Schema Objects
- •Schema Object Names and Qualifiers
- •Schema Object Naming Rules
- •Schema Object Naming Examples
- •Schema Object Naming Guidelines
- •Syntax for Schema Objects and Parts in SQL Statements
- •How Oracle Resolves Schema Object References
- •Referring to Objects in Other Schemas
- •Referring to Objects in Remote Databases
- •Referencing Object Type Attributes and Methods
- •3 Operators
- •About SQL Operators
- •Unary and Binary Operators
- •Operator Precedence
- •Arithmetic Operators
- •Concatenation Operator
- •Set Operators
- •4 Expressions
- •About SQL Expressions
- •Simple Expressions
- •Compound Expressions
- •CASE Expressions
- •CURSOR Expressions
- •Datetime Expressions
- •Function Expressions
- •INTERVAL Expressions
- •Object Access Expressions
- •Scalar Subquery Expressions
- •Type Constructor Expressions
- •Variable Expressions
- •Expression Lists
- •5 Conditions
- •About SQL Conditions
- •Condition Precedence
- •Comparison Conditions
- •Simple Comparison Conditions
- •Group Comparison Conditions
- •Logical Conditions
- •Membership Conditions
- •Range Conditions
- •Null Conditions
- •EQUALS_PATH
- •EXISTS Conditions
- •LIKE Conditions
- •IS OF type Conditions
- •UNDER_PATH
- •Compound Conditions
- •6 Functions
- •SQL Functions
- •Single-Row Functions
- •Aggregate Functions
- •Analytic Functions
- •Object Reference Functions
- •Alphabetical Listing of SQL Functions
- •ACOS
- •ADD_MONTHS
- •ASCII
- •ASCIISTR
- •ASIN
- •ATAN
- •ATAN2
- •BFILENAME
- •BITAND
- •CAST
- •CEIL
- •CHARTOROWID
- •COALESCE
- •COMPOSE
- •CONCAT
- •CONVERT
- •CORR
- •COSH
- •COUNT
- •COVAR_POP
- •COVAR_SAMP
- •CUME_DIST
- •CURRENT_DATE
- •CURRENT_TIMESTAMP
- •DBTIMEZONE
- •DECODE
- •DECOMPOSE
- •DENSE_RANK
- •DEPTH
- •DEREF
- •DUMP
- •EMPTY_BLOB, EMPTY_CLOB
- •EXISTSNODE
- •EXTRACT (datetime)
- •EXTRACT (XML)
- •EXTRACTVALUE
- •FIRST
- •FIRST_VALUE
- •FLOOR
- •FROM_TZ
- •GREATEST
- •GROUP_ID
- •GROUPING
- •GROUPING_ID
- •HEXTORAW
- •INITCAP
- •INSTR
- •LAST
- •LAST_DAY
- •LAST_VALUE
- •LEAD
- •LEAST
- •LENGTH
- •LOCALTIMESTAMP
- •LOWER
- •LPAD
- •LTRIM
- •MAKE_REF
- •MONTHS_BETWEEN
- •NCHR
- •NEW_TIME
- •NEXT_DAY
- •NLS_CHARSET_DECL_LEN
- •NLS_CHARSET_ID
- •NLS_CHARSET_NAME
- •NLS_INITCAP
- •NLS_LOWER
- •NLSSORT
- •NLS_UPPER
- •NTILE
- •NULLIF
- •NUMTODSINTERVAL
- •NUMTOYMINTERVAL
- •PATH
- •PERCENT_RANK
- •PERCENTILE_CONT
- •PERCENTILE_DISC
- •POWER
- •RANK
- •RATIO_TO_REPORT
- •RAWTOHEX
- •RAWTONHEX
- •REFTOHEX
- •REGR_ (Linear Regression) Functions
- •REPLACE
- •ROUND (number)
- •ROUND (date)
- •ROW_NUMBER
- •ROWIDTOCHAR
- •ROWIDTONCHAR
- •RPAD
- •RTRIM
- •SESSIONTIMEZONE
- •SIGN
- •SINH
- •SOUNDEX
- •SQRT
- •STDDEV
- •STDDEV_POP
- •STDDEV_SAMP
- •SUBSTR
- •SYS_CONNECT_BY_PATH
- •SYS_CONTEXT
- •SYS_DBURIGEN
- •SYS_EXTRACT_UTC
- •SYS_GUID
- •SYS_TYPEID
- •SYS_XMLAGG
- •SYS_XMLGEN
- •SYSDATE
- •SYSTIMESTAMP
- •TANH
- •TO_CHAR (character)
- •TO_CHAR (datetime)
- •TO_CHAR (number)
- •TO_CLOB
- •TO_DATE
- •TO_DSINTERVAL
- •TO_MULTI_BYTE
- •TO_NCHAR (character)
- •TO_NCHAR (datetime)
- •TO_NCHAR (number)
- •TO_NCLOB
- •TO_NUMBER
- •TO_SINGLE_BYTE
- •TO_TIMESTAMP
- •TO_TIMESTAMP_TZ
- •TO_YMINTERVAL
- •TRANSLATE
- •TRANSLATE ... USING
- •TREAT
- •TRIM
- •TRUNC (number)
- •TRUNC (date)
- •TZ_OFFSET
- •UNISTR
- •UPDATEXML
- •UPPER
- •USER
- •USERENV
- •VALUE
- •VAR_SAMP
- •VARIANCE
- •VSIZE
- •WIDTH_BUCKET
- •XMLAGG
- •XMLCOLATTVAL
- •XMLCONCAT
- •XMLELEMENT
- •XMLFOREST
- •XMLSEQUENCE
- •XMLTRANSFORM
- •ROUND and TRUNC Date Functions
- •User-Defined Functions
- •Prerequisites
- •Name Precedence
- •7 Common SQL DDL Clauses
- •allocate_extent_clause
- •constraints
- •deallocate_unused_clause
- •file_specification
- •logging_clause
- •parallel_clause
- •physical_attributes_clause
- •storage_clause
- •8 SQL Queries and Subqueries
- •About Queries and Subqueries
- •Creating Simple Queries
- •Hierarchical Queries
- •The UNION [ALL], INTERSECT, MINUS Operators
- •Sorting Query Results
- •Joins
- •Using Subqueries
- •Unnesting of Nested Subqueries
- •Selecting from the DUAL Table
- •Distributed Queries
- •9 SQL Statements: ALTER CLUSTER to ALTER SEQUENCE
- •Types of SQL Statements
- •Organization of SQL Statements
- •ALTER CLUSTER
- •ALTER DATABASE
- •ALTER DIMENSION
- •ALTER FUNCTION
- •ALTER INDEX
- •ALTER INDEXTYPE
- •ALTER JAVA
- •ALTER MATERIALIZED VIEW
- •ALTER MATERIALIZED VIEW LOG
- •ALTER OPERATOR
- •ALTER OUTLINE
- •ALTER PACKAGE
- •ALTER PROCEDURE
- •ALTER PROFILE
- •ALTER RESOURCE COST
- •ALTER ROLE
- •ALTER ROLLBACK SEGMENT
- •ALTER SEQUENCE
- •10 SQL Statements: ALTER SESSION to ALTER SYSTEM
- •ALTER SESSION
- •ALTER SYSTEM
- •ALTER TABLE
- •ALTER TABLESPACE
- •ALTER TRIGGER
- •ALTER TYPE
- •ALTER USER
- •ALTER VIEW
- •ANALYZE
- •ASSOCIATE STATISTICS
- •AUDIT
- •CALL
- •COMMENT
- •COMMIT
- •13 SQL Statements: CREATE CLUSTER to CREATE JAVA
- •CREATE CLUSTER
- •CREATE CONTEXT
- •CREATE CONTROLFILE
- •CREATE DATABASE
- •CREATE DATABASE LINK
- •CREATE DIMENSION
- •CREATE DIRECTORY
- •CREATE FUNCTION
- •CREATE INDEX
- •CREATE INDEXTYPE
- •CREATE JAVA
- •14 SQL Statements: CREATE LIBRARY to CREATE SPFILE
- •CREATE LIBRARY
- •CREATE MATERIALIZED VIEW
- •CREATE MATERIALIZED VIEW LOG
- •CREATE OPERATOR
- •CREATE OUTLINE
- •CREATE PACKAGE
- •CREATE PACKAGE BODY
- •CREATE PFILE
- •CREATE PROCEDURE
- •CREATE PROFILE
- •CREATE ROLE
- •CREATE ROLLBACK SEGMENT
- •CREATE SCHEMA
- •CREATE SEQUENCE
- •CREATE SPFILE
- •15 SQL Statements: CREATE SYNONYM to CREATE TRIGGER
- •CREATE SYNONYM
- •CREATE TABLE
- •CREATE TABLESPACE
- •CREATE TEMPORARY TABLESPACE
- •CREATE TRIGGER
- •CREATE TYPE
- •CREATE TYPE BODY
- •CREATE USER
- •CREATE VIEW
- •DELETE
- •DISASSOCIATE STATISTICS
- •DROP CLUSTER
- •DROP CONTEXT
- •DROP DATABASE LINK
- •DROP DIMENSION
- •DROP DIRECTORY
- •DROP FUNCTION
- •DROP INDEX
- •DROP INDEXTYPE
- •DROP JAVA
- •DROP LIBRARY
- •DROP MATERIALIZED VIEW
- •DROP MATERIALIZED VIEW LOG
- •DROP OPERATOR
- •DROP OUTLINE
- •DROP PACKAGE
- •DROP PROCEDURE
- •DROP PROFILE
- •DROP ROLE
- •DROP ROLLBACK SEGMENT
- •17 SQL Statements: DROP SEQUENCE to ROLLBACK
- •DROP SEQUENCE
- •DROP SYNONYM
- •DROP TABLE
- •DROP TABLESPACE
- •DROP TRIGGER
- •DROP TYPE
- •DROP TYPE BODY
- •DROP USER
- •DROP VIEW
- •EXPLAIN PLAN
- •GRANT
- •INSERT
- •LOCK TABLE
- •MERGE
- •NOAUDIT
- •RENAME
- •REVOKE
- •ROLLBACK
- •18 SQL Statements: SAVEPOINT to UPDATE
- •SAVEPOINT
- •SELECT
- •SET CONSTRAINT[S]
- •SET ROLE
- •SET TRANSACTION
- •TRUNCATE
- •UPDATE
- •Required Keywords and Parameters
- •Optional Keywords and Parameters
- •Syntax Loops
- •Multipart Diagrams
- •Database Objects
- •ANSI Standards
- •ISO Standards
- •Oracle Compliance
- •FIPS Compliance
- •Oracle Extensions to Standard SQL
- •Character Set Support
- •Using Extensible Indexing
- •Using XML in SQL Statements
- •Index

SYS_CONNECT_BY_PATH
manager that are equal to or less than the current salary. You can see that Raphaely and Cambrault have the same cumulative total. This is because Raphaely and Cambrault have the identical salaries, so Oracle adds together their salary values and applies the same cumulative total to both rows.
SELECT manager_id, last_name, salary,
SUM(salary) OVER (PARTITION BY manager_id ORDER BY salary
RANGE UNBOUNDED PRECEDING) l_csum
FROM employees;
MANAGER_ID |
LAST_NAME |
SALARY |
L_CSUM |
|
---------- |
--------------- ---------- ---------- |
|||
|
100 |
Mourgos |
5800 |
5800 |
|
100 |
Vollman |
6500 |
12300 |
|
100 |
Kaufling |
7900 |
20200 |
|
100 |
Weiss |
8000 |
28200 |
|
100 |
Fripp |
8200 |
36400 |
|
100 |
Zlotkey |
10500 |
46900 |
|
100 |
Raphaely |
11000 |
68900 |
|
100 |
Cambrault |
11000 |
68900 |
|
100 |
Errazuriz |
12000 |
80900 |
. |
|
|
|
|
. |
|
|
|
|
. |
149 |
Taylor |
8600 |
30200 |
|
||||
|
149 |
Hutton |
8800 |
39000 |
|
149 |
Abel |
11000 |
50000 |
|
201 |
Fay |
6000 |
6000 |
|
205 |
Gietz |
8300 |
8300 |
|
|
King |
24000 |
24000 |
SYS_CONNECT_BY_PATH
Syntax sys_connect_by_path::=
SYS_CONNECT_BY_PATH (
column
,
char
)
Purpose
SYS_CONNECT_BY_PATH is valid only in hierarchical queries. It returns the path of a column value from root to node, with column values separated by char for each row returned by CONNECT BY condition.
Functions 6-155

SYS_CONTEXT
Both column and char can be any of the datatypes CHAR, VARCHAR2, NCHAR, or NVARCHAR2. The string returned is of VARCHAR2 datatype and is in the same character set as column.
See Also: "Hierarchical Queries" on page 8-3 for more information about hierarchical queries and CONNECT BY conditions
Examples
The following example returns the path of employee names from employee Kochhar to all employees of Kochhar (and their employees):
SELECT LPAD(’ ’, 2*level-1)||SYS_CONNECT_BY_PATH(last_name, ’/’) "Path" FROM employees
START WITH last_name = ’Kochhar’
CONNECT BY PRIOR employee_id = manager_id;
Path
---------------------------------------------------------------
/Kochhar
/Kochhar/Greenberg
/Kochhar/Greenberg/Faviet
/Kochhar/Greenberg/Chen
/Kochhar/Greenberg/Sciarra
/Kochhar/Greenberg/Urman
/Kochhar/Greenberg/Popp
/Kochhar/Whalen
/Kochhar/Mavris
/Kochhar/Baer
/Kochhar/Higgins
/Kochhar/Higgins/Gietz
SYS_CONTEXT
Syntax sys_context::=
|
|
|
|
|
|
|
|
, |
length |
SYS_CONTEXT |
( |
’ |
namespace |
’ |
, |
’ |
parameter |
’ |
) |
6-156 Oracle9i SQL Reference

SYS_CONTEXT
Purpose
SYS_CONTEXT returns the value of parameter associated with the context namespace. You can use this function in both SQL and PL/SQL statements.
For namespace and parameter, you can specify either a string (constant) or an expression that resolves to a string designating a namespace or an attribute. The context namespace must already have been created, and the associated parameter and its value must also have been set using the DBMS_SESSION.set_ context procedure. The namespace must be a valid SQL identifier. The parameter name can be any string. It is not case sensitive, but it cannot exceed 30 bytes in length.
The datatype of the return value is VARCHAR2. The default maximum size of the return value is 256 bytes. You can override this default by specifying the optional length parameter. The valid range of values is 1 to 4000 bytes. (If you specify an invalid value, then Oracle ignores it and uses the default.)
Oracle9i provides a built-in namespace called USERENV, which describes the current session. The predefined parameters of namespace USERENV are listed Table 6–2 on page 6-158, along with the lengths of their return strings.
Restriction on SYS_CONTEXT You cannot specify this function in a parallel query.
See Also:
■Oracle9i Application Developer’s Guide - Fundamentals for information on using the application context feature in your application development
■CREATE CONTEXT on page 13-12 for information on creating user-defined context namespaces
■Oracle9i Supplied PL/SQL Packages and Types Reference for information on the DBMS_SESSION.set_context procedure
Examples
The following statement returns the name of the user who logged onto the database:
CONNECT OE/OE
SELECT SYS_CONTEXT (’USERENV’, ’SESSION_USER’)
FROM DUAL;
SYS_CONTEXT (’USERENV’, ’SESSION_USER’)
------------------------------------------------------
Functions 6-157

SYS_CONTEXT
OE
The following hypothetical example returns the group number that was set as the value for the attribute group_no in the PL/SQL package that was associated with the context hr_apps when hr_apps was created:
SELECT SYS_CONTEXT (’hr_apps’, ’group_no’) "User Group"
FROM DUAL;
Table 6–2 Predefined Parameters of Namespace USERENV
|
|
Return |
|
|
Length |
Parameter |
Return Value |
(bytes) |
|
|
|
AUDITED_CURSORID |
Returns the cursor ID of the SQL that triggered the |
NA |
|
audit. |
|
|
|
|
AUTHENTICATION_DATA |
Data being used to authenticate the login user. For |
256 |
|
X.503 certificate authenticated sessions, this field |
|
|
returns the context of the certificate in HEX2 format. |
|
|
Note: You can change the return value of the |
|
|
AUTHENTICATION_DATA attribute using the length |
|
|
parameter of the syntax. Values of up to 4000 are |
|
|
accepted. This is the only attribute of USERENV for |
|
|
which Oracle implements such a change. |
|
|
|
|
AUTHENTICATION_TYPE |
How the user was authenticated: |
30 |
|
■ DATABASE: user name/password authentication |
|
|
■ OS: operating system external user |
|
|
authentication |
|
|
■ NETWORK: network protocol or ANO |
|
|
authentication |
|
|
■ PROXY: OCI proxy connection authentication |
|
|
|
|
BG_JOB_ID |
Job ID of the current session if it was established by |
64 |
|
an Oracle background process. Null if the session |
|
|
was not established by a background process. |
|
|
|
|
CLIENT_IDENTIFIER |
Returns the client session identifier in the global |
NA |
|
context—that is, the globally accessed application |
|
|
context or (in the OCI context) the OCI_ATTR_ |
|
|
CLIENT_IDENTIFIER attribute. If no globally |
|
|
relevant identifier has been set, returns null. |
|
|
|
|
CLIENT_INFO |
Returns up to 64 bytes of user session information |
64 |
|
that can be stored by an application using the DBMS_ |
|
|
APPLICATION_INFO package. |
|
|
|
|
6-158 Oracle9i SQL Reference

SYS_CONTEXT
Table 6–2 (Cont.) Predefined Parameters of Namespace USERENV
|
|
Return |
|
|
Length |
Parameter |
Return Value |
(bytes) |
|
|
|
CURRENT_SCHEMA |
Name of the default schema being used in the |
30 |
|
current schema. This value can be changed during |
|
|
the session with an ALTER SESSION SET CURRENT_ |
|
|
SCHEMA statement. |
|
|
|
|
CURRENT_SCHEMAID |
Identifier of the default schema being used in the |
30 |
|
current session. |
|
|
|
|
CURRENT_SQL |
Returns the current SQL that triggered the |
64 |
|
fine-grained auditing event. You can specify this |
|
|
attribute only inside the event handler for the |
|
|
Fine-Grained Auditing feature. |
|
|
|
|
CURRENT_USER |
The name of the user whose privilege the current |
30 |
|
session is under. |
|
|
|
|
CURRENT_USERID |
User ID of the user whose privilege the current |
30 |
|
session is under. |
|
|
|
|
DB_DOMAIN |
Domain of the database as specified in the DB_ |
256 |
|
DOMAIN initialization parameter. |
|
|
|
|
DB_NAME |
Name of the database as specified in the DB_NAME |
30 |
|
initialization parameter. |
|
|
|
|
ENTRY_ID |
The available auditing entry identifier. You cannot |
30 |
|
use this attribute in distributed SQL statements. To |
|
|
use this keyword in USERENV, the initialization |
|
|
parameter AUDIT_TRAIL must be set to TRUE. |
|
|
|
|
EXTERNAL_NAME |
External name of the database user. For SSL |
256 |
|
authenticated sessions using v.503 certificates, this |
|
|
field returns the distinguished name (DN) stored in |
|
|
the user certificate. |
|
|
|
|
FG_JOB_ID |
Job ID of the current session if it was established by a |
30 |
|
client foreground process. Null if the session was not |
|
|
established by a foreground process. |
|
|
|
|
GLOBAL_CONTEXT_ |
Returns the number being used in the System Global |
NA |
MEMORY |
Area by the globally accessed context. |
|
|
|
|
HOST |
Name of the host machine from which the client has |
54 |
|
connected. |
|
|
|
|
INSTANCE |
The instance identification number of the current |
30 |
|
instance. |
|
|
|
|
Functions 6-159

SYS_CONTEXT
Table 6–2 (Cont.) Predefined Parameters of Namespace USERENV
|
|
Return |
|
|
Length |
Parameter |
Return Value |
(bytes) |
|
|
|
IP_ADDRESS |
IP address of the machine from which the client is |
30 |
|
connected. |
|
|
|
|
ISDBA |
Returns TRUE if the user has been authenticated as |
30 |
|
having DBA privileges either through the operating |
|
|
system or through a password file. |
|
|
|
|
LANG |
The ISO abbreviation for the language name, a |
62 |
|
shorter form than the existing ’LANGUAGE’ |
|
|
parameter. |
|
|
|
|
LANGUAGE |
The language and territory currently used by your |
52 |
|
session, along with the database character set, in this |
|
|
form: |
|
|
language_territory.characterset |
|
|
|
|
NETWORK_PROTOCOL |
Network protocol being used for communication, as |
256 |
|
specified in the ’PROTOCOL=protocol’ portion of |
|
|
the connect string. |
|
|
|
|
NLS_CALENDAR |
The current calendar of the current session. |
62 |
|
|
|
NLS_CURRENCY |
The currency of the current session. |
62 |
|
|
|
NLS_DATE_FORMAT |
The date format for the session. |
62 |
|
|
|
NLS_DATE_LANGUAGE |
The language used for expressing dates. |
62 |
|
|
|
NLS_SORT |
BINARY or the linguistic sort basis. |
62 |
|
|
|
NLS_TERRITORY |
The territory of the current session. |
62 |
|
|
|
OS_USER |
Operating system user name of the client process that |
30 |
|
initiated the database session. |
|
|
|
|
PROXY_USER |
Name of the database user who opened the current |
30 |
|
session on behalf of SESSION_USER. |
|
|
|
|
PROXY_USERID |
Identifier of the database user who opened the |
30 |
|
current session on behalf of SESSION_USER. |
|
|
|
|
SESSION_USER |
Database user name by which the current user is |
30 |
|
authenticated. This value remains the same |
|
|
throughout the duration of the session. |
|
|
|
|
SESSION_USERID |
Identifier of the database user name by which the |
30 |
|
current user is authenticated. |
|
|
|
|
6-160 Oracle9i SQL Reference