Oracle Miscellaneous Functions

点击打开链接

Oracle Miscellaneous Functions
Version 11.1
General information
Note:These are functions not covered on other site pages
LNNVL

Evaluates a condition when one or both operands of the condition may be null
LNNVL(<condition>)
conn hr/hr

SELECTCOUNT(*)FROMemployeesWHEREcommission_pct >= .2;

SELECTCOUNT(*)FROMemployeesWHERELNNVL(commission_pct >= .2);
NULLIF

Compares expr1 and expr2. If they are equal,then the function returns null. If they are not equal,then the function returns expr1. You cannot specify the literal NULL for expr1.

NULLIF(<expression1>,<expression2>)
conn hr/hr

SELECTe.last_name,NULLIF(e.job_id,j.job_id) "OLD JOB ID"
FROMemployees e,job_history j
WHEREe.employee_id = j.employee_id
ORDER BYlast_name;
NVL

Returns a Value if the Expression IS NULL
NVL(
s1VARCHAR2CHaraCTER SET ANY_CS,-- expression
s2VARCHAR2CHaraCTER SET s1%CHARSET) -- return value if null
RETURNVARCHAR2CHaraCTER SET s1%CHARSET;

NVL(B1 BOOLEAN,B2 BOOLEAN) return BOOLEAN;
set serveroutput on

DECLARE
iPLS_INTEGER;
BEGIN
SELECTNVL(i,93)
INTOi
FROMDUAL;

dbms_output.put_line('i1: ' || i);

SELECTNVL(i,39)
INTOi
FROMDUAL;

dbms_output.put_line('i2: ' || i);
END;
/
NVL2

Returns First Value if NOT NULL,Second Value if NULL

Thanks Cary Hogan and Kaifer Bohus for the corrections
NVL2(<expression>,<return_if_not_null>,<return_if_null>)
CREATETABLEtest (
categoryVARCHAR2(20),
outvalNUMBER(3),
invalNUMBER(3));

INSERTINTOtestVALUES('Groceries',10,NULL);
INSERTINTOtestVALUES('Payroll',NULL,100);
INSERTINTOtestVALUES('Groceries',20,200);
INSERTINTOtestVALUES('Groceries',30,NULL);

SELECT*FROMtest;

SELECTcategory,SUM(NVL2(outval,-outval,inval)) NET
FROMtest
GROUP BYcategory;

Note: If used in PL/sql must be used in the form ofSELECTINTO
thus you can not use this Syntax:

set serveroutput on

DECLARE
xNUMBER(5);
BEGIN
x := NVL2(10,20);
dbms_output.put_line(TO_CHAR(x));
END;
/

but you can write:

DECLARE
xNUMBER(5);
BEGIN
SELECTNVL2(10,20)
INTOx
FROMDUAL;

dbms_output.put_line(TO_CHAR(x));
END;
/
SQLCODE

Number of the most recent exception raised by PL/sql. 0 if none
standard.sqlcodeRETURNPLS_INTEGER;
set serveroutput on

BEGIN
dbms_output.put_line(sqlCODE);
END;
/

See Exceptions Page
SQLERRM

Error message associated with the specified code
standard.sqlerrm RETURNVARCHAR2;

standard.sqlerrm(code_in IN INTEGER := sqlCODE)RETURNVARCHAR2
set serveroutput on

BEGIN
dbms_output.put_line(sqlERRM);
END;
/

See Exceptions Page
SQL_GUID

Generates and returns a globally unique identifier (RAW value) made up of 16 bytes. On most platforms,the generated identifier consists of a host identifier,a process or thread identifier of the process or thread invoking the function,and a nonrepeating value (sequence of bytes) for that process or thread.
SYS_GUID()RETURNRAW
CREATETABLEt (
ridRAW(32),
colVARCHAR2(20));

desc t

INSERTINTOt
(rid,col)
VALUES
(SYS_GUID(),'ABC');

INSERTINTOt
(rid,'DEF');

SELECT*FROMt;
SYS_TYPEID

Returns the typeid of the most specific type of the operand
SYS_TYPEID(<object_type_value>)
CREATETYPEperson_tASOBJECT (nameVARCHAR2(30),ssnNUMBER)
NOTFINAL;
/

CREATETABLEpersons OF person_t;

INSERTINTOpersons
VALUES
(person_t('Morgan',123));

SELECTname,SYS_TYPEID(VALUE(p)) TYPE_IDFROMpersons p;
UID
User Session ID SELECTUID
FROMDUAL;

SELECTuser#
FROMgv$session
WHEREschemaname = USER;
USER
User As Logged On SELECTUSERFROMDUAL;
USERENV(deprecated: use SYS_CONTEXT)
Usage SELECTuserenv('<parameter>')
FROMDUAL;
Session info. stored with DBMS_APPLICATION_INFO SELECTUSERENV('CLIENT_INFO')FROMDUAL;

exec dbms_application_info.set_client_info('TEST');

SELECTUSERENV('CLIENT_INFO')FROMDUAL;
The current audit entry number. The audit entryid sequence is shared between fine-grained audit records and regular audit records. You cannot use this attribute in distributed sql statements. SELECTuserenv('ENTRYID')
FROMDUAL;
Current instance identifier SELECTuserenv('INSTANCE')
FROMDUAL;
ISDBA returns 'TRUE' if the user has been authenticated as having DBA privileges either through the operating system or through a password file. SELECTuserenv('ISDBA')
FROMDUAL;
The ISO abbreviation for the language name,a shorter form than the existing 'LANGUAGE' parameter. SELECTuserenv('LANG')
FROMDUAL;
The language and territory currently used by your session,along with the database character set,in the form:
language_territory dot characterset.
SELECTuserenv('LANGUAGE')
FROMDUAL;
The auditing session identifier. You cannot use this option in distributed sql statements. SELECTuserenv('SESSIONID')
FROMDUAL;

SELECTaudsid
FROMv_$session;
TERMINAL returns the operating system identifier for the terminal of the current session. In distributed sql statements,this parameter returns the identifier for your local session. In a distributed environment,this parameter is supported only for remote SELECT statements,not for remote INSERT,UPDATE,or DELETE operations. SELECTuserenv('TERMINAL')
FROMDUAL;
VALUE

Takes as its argument a correlation variable (table alias) associated with a row of an object table and returns object instances stored in the object table. The type of the object instances is the same type as the object table.
VALUE(correlation_variable)
CREATETYPEaddress_tASOBJECT (
hnoNUMBER,
streetVARCHAR2(40),
cityVARCHAR2(20),
zipVARCHAR2(5),
phoneVARCHAR2(10));
/

CREATETYPEpersonASOBJECT (
nameVARCHAR2(40),
dateofbirthDATE,
homeaddress address_t,
manager REF person);
/

CREATEORREPLACETYPEperson_tASOBJECT (
nameVARCHAR2(100),
ssnNUMBER)
NOTFINAL;
/

CREATETABLEpersons OF person_t;

INSERTINTOpersonsVALUES(person_t('Bob',1234));

SELECTVALUE(p)FROMpersons p;

相关文章

Java Oracle 结果集是Java语言中处理数据库查询结果的一种方...
Java AES和Oracle AES是现代加密技术中最常使用的两种AES加密...
Java是一种广泛应用的编程语言,具备可靠性、安全性、跨平台...
随着移动互联网的发展,抽奖活动成为了营销活动中不可或缺的...
Java和Oracle都是在计算机领域应用非常广泛的技术,他们经常...
Java 是一门非常流行的编程语言,它可以运行于各种操作系统上...