Oracle 11 G Vs 12 C
Oracle 11 G Vs 12 C
Oracle 11 G Vs 12 C
Oracle has announced the new version Oracle 11 G and announced the new features in
that version. Oracle has added around 500 Oracle 11 G New Features for DBA.(exact
number is 482 features).The Most exciting features are DBA related features and simplicity
and well-structured DBA tasks. Another important Oracle 11 G New Feature is related to
Performance Tuning and optimization. Oracle has added considerable enhancements in
SQL to raise the performance of SQL Query. In this article, will explain the extra features
related to SQL. I am writing this article to give idea about the Oracle 11 G New Features
with examples. These Oracle 11 G New Features are important for programmers as well as
DBA to use it for different purpose.
Scenario:
Add the New column in Employee table in which Employee_Status is ‘Working’
Query:
Alter table Employee
v_start := DBMS_UTILITY.get_time;
END P_Simple_Integer_test;
Output:
PLS_INTEGER: 19 hsecs
SIMPLE_INTEGER: 12 hsecs
3.Invisible Indexes:
Indexes can be created in Invisible mode in Oracle 11 G.The new parameter has been
introduced named OPTIMIZER_USE_INVISIBLE_INDEXES and optimizer uses the same
parameter if it sets to ‘TRUE’ or ‘FALSE’.This parameter sets to true or false at system
level.
Syntax:
Create or replace index Index_name on Table_name(Column_name) Invisible;
Alter index:
ALTER Index Indexname set Invisible=[TRUE|FALSE];
4.SQL /*result_cache*/ Hint:
The result data is cached in the data buffer using this hint.This hint improves the
performance of query drastically as the result data is stored in data buffer the data is been
fetched directly from the buffer location.The RESULT_CACHE_MODE parameter specifies
the applicability of result cache feature in SQL queries.It accepts two admissible values
‘Manual’ and ‘Force’.For MANUAL mode, RESULT_CACHE hint must be specified with the
SQL statements to use the feature. In FORCE mode, server enables the caching feature
with all the SQL statements.
Syntax:
ALTER SYSTEM SET RESULT_CACHE_MODE=’MANUAL’;
Example of Hint:
Select /*+result_cache*/ Department_ID,Dept_num
from Department;
5.Read Only Mode of Tables:
Oracle 11G provides the facility to change the mode of the table to Read only mode.In
read_only mode table can only be queried.The DML and DDL (Truncate and Alter) options
are restricted in read_only mode.At any point of time the table mode has been changed
from Read mode to write mode.
Syntax:
ALTER TABLE TABLE_NAME [READ ONLY|READ WRITE];
Query to Find Second highest salary of Employee with explaination..
6.Regular Expression (REGEXP_COUNT function):
REGEXP_COUNT is the new regular expression function, introduced in Oracle 11g release.
This function is introduced for language support which is used to count the character or
string appearances in a given string.
Example:
SELECT ‘Rohit Shinde’ Programmer,REGEXP_COUNT(Programmer, ‘s’, 1, ‘i’)
‘Expression_Output’ from dual;
Output:
Programmer Expression_Output
Rohit Shinde 1
7.Virtual Columns:
Oracle 11 G provides new feature of virtual columns which allows user to provide the
column virtually where value is specified in Expression itself.Virtual columns acts like a
normal columns during indexing and partitioning.Virtual columns belongs to LOBs or
collection datatypes.
Example:
9.Partitioning:
The partitioning plays an important role in database performance and every SQL
professional wants to do the perfect partitioning to improve the performance of SQL
queries.There are following new features added in SQL partitioning:
1.System Partitioning
2.Reference Partitioning
3.Interval Partitioning
10.Analytical Functions-NTH,LISTAGG:
Oracle 11 G specifies two new aggregate functions:
1.LISTAGG:
LISTAGG aggregates a column values in a single row format.
2.NTH_VALUE:
NTH_VALUE is an extended format of FIRST_VALUE and LAST_VALUE functions to get a
random row from a grouped result set.
Syntax:
LISTAGG – LISTAGG (measure_expr [, ‘delimiter_expr’]) WITHIN GROUP (ORDER
BY clause) [OVER PARTITION BYclause]
Real Life Scenario:
Kindly consider following table.We need to find aggregate results departmentwise in form of
rows.
Employee table:
Department ID Employee_name
100 Amit,Rohan
101 Rohit
So listagg function is very useful function for converting columns in to single row format.
for update
Skip locked;
12.XML SQL queries – Oracle11g will support query mechanisms for XML including XQuery
and SQL XML, emerging standards for querying XML data stored inside tables.
Here I have tried to cover the Newly added features in Oracle 11G. Please do let me know if
any other information needs to be added in this.Please comment below if you needed extra
information or suggestions if any..
Oracle 11g vs 12c :
In my previous article, I have given the idea about the Oracle 11g new features. There are some
newly added features in Oracle 12c over oracle 11G.In the article I will try to explain the newly
added features in oracle 12c as well as Oracle 11g vs 12c. From 12c Onwards, the instance is shared
with multiple databases. This multiple databases are self-contained and pluggable from one database
to another database. This is very useful methodology where database consolidation. In short a single
SGA and background process will be shared to multiple databases, the databases can be created on
fly and drop or attach and detach from one server to another server.This article will give you idea
about Oracle 11g vs 12c .
Oracle 12c Features:
1.Advanced Indexing Techniques:
Oracle 12c user will be able to create multiple indexes on same column.Prior to oracle 12c, user will
be able to create single index on one column. While creating multiple index-using oracle 12c the type
of index should be different and only one type of index is usable at a time. Means if user create one
index on column ‘a’ which is simple index and usable then user cannot create the index on same
column.
To create multiple index on it column alter the session and set following attribute as true:
Optimizer_use_invisible_index=true
6.JSON support :
Oracle 12C supports the Java Object Notation(JSON) data to Oracle Database and allows
the database to enforce that JSON stored in the Oracle Database conforms to the JSON
rules.
8.Invisible Columns :
Oracle 12c introduces new feature of invisible columns in the table.When specific column is
defined as invisible it will not appear in generic queries.It is pretty easy to add invisible
columns or modify invisible columns.
Example :
Create table Employee
(Employee_no Number(5),
Newly added Functions: The newly added function are not Newly added Functions: Oracle 12C has added
considering the performance tuning of the data. The newly added the new feature approx_count_distinct () which
functions in oracle 11G are LISTAGG and Nth_value functions. provides approximate count distinct aggregation.
In Memory table :
Oracle 11G supports the concept of In-memory tables, which
will, used to improve the full table scans. The user needs to put
the table in the memory so that user can access the fast data. In Memory aggregation :
Oracle 12C supports the In memory aggregation
Oracle 11G does not support the In memory aggregation concept. concept which is very useful in star queries.
6.What is Query to display last 5 Records from Employee table?(90% asked Complex SQL
Queries Examples)
Answer:
Select * from Employee e where rownum <=5
union
select * from (Select * from Employee e order by rowid desc) where rownum <=5;
Click Here to get What is Union?
7.What is Query to display Nth Record from Employee table?
Answer :
select * from ( select a.*, rownum rnum from ( YOUR_QUERY_GOES_HERE — including
the order by ) a where rownum <= N_ROWS ) where rnum >= N_ROWS
8.How to get 3 Highest salaries records from Employee table?
Answer:
select distinct salary from employee a where 3 >= (select count(distinct salary) from
employee b where a.salary <= b.salary) order by a.salary desc;
Alternative Solution: Suggested by Ankit Srivastava
minus
22.How to fetch all the records from Employee whose joining year is 2017?
Answer:
Oracle:
Group by rollno
JSON_QUERY
Note:
The JSON_QUERY function is available starting with Oracle Database 12c Release 1 (12.1.0.2).
Syntax
JSON_path_expression::=
object_step::=
Description of the illustration ''object_step.gif''
array_step::=
JSON_query_returning_clause::=
JSON_query_return_type::=
JSON_query_wrapper_clause::=
Description of the illustration ''json_query_wrapper_clause.gif''
JSON_query_on_error_clause::=
Purpose
JSON_QUERY finds one or more specified JSON values in JSON data and returns the values in a character
string.
expr
Use this clause to specify the JSON data to be evaluated. For expr, specify an expression that evaluates to a
text literal. If expr is a column, then the column must be of data type VARCHAR2, CLOB, or BLOB. If expr is
null, then the function returns null.
If expr is not a text literal of well-formed JSON data using strict or lax syntax, then the function returns null
by default. You can use the JSON_query_on_error_clause to override this default behavior. Refer
to JSON_query_on_error_clause.
FORMAT JSON
JSON_path_expression
Use this clause to specify a JSON path expression. The function uses the path expression to evaluate expr and
find one or more JSON values that match, or satisfy, the path expression. The path expression must be a text
literal.
The path expression must begin with a dollar sign ($), which represents the context item, that is, the expression
specified by expr. The dollar sign is followed by zero or more steps, each of which can be an object step or an
array step.
The function attempts to match the first step in the path expression to the context item. If the first step results
in a match, then the function attempts to match the second step to the JSON value(s) that matched the first
step. If the second step results in a match, then the function attempts to match the third step to the JSON
values(s) that matched the second step, and so on. The function returns the value(s) matched in the final step as
a comma-separated sequence of values in a character string. The order of the sequence is nondeterministic. All
values are returned using strict JSON syntax, regardless of whether the original JSON data used strict or lax
JSON syntax. A path expression that consists of a dollar sign followed by zero steps ( '$') matches the entire
context item.
You can specify the JSON_query_returning_clause to control the data type and format of the return
character string. Refer to the JSON_query_returning_clause.
If multiple values match the path expression, or if only one scalar value matches the path expression, then you
must wrap the value(s) in an array wrapper. Refer to the JSON_query_wrapper_clause.
If any step in the path expression does not result in a match, then the function returns null by default. You can
use the JSON_query_on_error_clause to override this default behavior. Refer to
the JSON_query_on_error_clause.
A simple_name can contain only alphanumeric characters and must begin with an alphabetic
character. A complex_name can contain only alphanumeric characters and spaces, and must begin
with an alphanumeric character. A complex_name must be enclosed in double quotation marks.
Use the asterisk wildcard symbol (*) to specify all property names. If the JSON object being evaluated
contains at least one member, then the object step results in a match to the values of all members.
Otherwise, the object step does not result in a match.
If you apply an object step to a JSON array, then the array is implicitly unwrapped and the elements of the
array are evaluated using the object step. This is called JSON path expression relaxation. Refer to Oracle XML
DB Developer's Guide for more information.
If the JSON data being evaluated is not a JSON object, then the object step does not result in a match.
Use the asterisk wildcard symbol (*) to specify all elements in a JSON array. If the JSON array being
evaluated contains at least one element, then the array step results in a match to all elements in the
JSON array. Otherwise, the array step does not result in a match.
If the JSON data being evaluated is not a JSON array, then the data is implicitly wrapped in an array and then
evaluated using the array step. This is called JSON path expression relaxation. Refer to Oracle XML DB
Developer's Guide for more information.
JSON_query_returning_clause
Use this clause to specify the data type and format of the character string returned by this function.
RETURNING Use the RETURNING clause to specify the data type of the character string. If you omit this
clause, then JSON_QUERY returns a character string of type VARCHAR2(4000).
VARCHAR2[(size [BYTE,CHAR])]
When specifying the VARCHAR2 data type elsewhere in SQL, you are required to specify a size.
However, in this clause you can omit the size. In this case, JSON_QUERY returns a character string of
type VARCHAR2(4000).
If the data type is not large enough to hold the return character string, then this function returns null by default.
You can use the JSON_query_on_error_clause to override this default behavior. Refer to
the JSON_query_on_error_clause.
PRETTY Specify PRETTY to pretty-print the return character string by inserting newline characters and
indenting.
ASCII Specify ASCII to automatically escape any non-ASCII Unicode characters in the return character
string, using standard ASCII Unicode escape sequences.
JSON_query_wrapper_clause
Use this clause to control whether this function wraps the values matched by the path expression in an array
wrapper—that is, encloses the sequence of values in square brackets ([]).
Specify WITHOUT WRAPPER to omit the array wrapper. You can specify this clause only if the path
expression matches a single JSON object or JSON array. This is the default.
Specify WITH WRAPPER to include the array wrapper. You must specify this clause if the path
expression matches a single scalar value (a value that is not a JSON object or JSON array) or multiple
values of any type.
Specifying the WITH UNCONDITIONAL WRAPPER clause is equivalent to specifying
the WITH WRAPPER clause. The UNCONDITIONAL keyword is provided for semantic clarity.
If the function returns a single scalar value, or multiple values of any type, and you do not
specify WITH [UNCONDITIONAL | CONDITIONAL] WRAPPER, then the function returns null by default. You
can use the JSON_query_on_error_clause to override this default behavior. Refer to
the JSON_query_on_error_clause.
JSON_query_on_error_clause
Use this clause to specify the value returned by this function when any of the following errors occur:
expr is not well-formed JSON data using strict or lax JSON syntax
No match is found when the JSON data is evaluated using the JSON path expression
The return value data type is not large enough to hold the return character string
The function matches a single scalar value or, multiple values of any type, and
the WITH [UNCONDITIONAL | CONDITIONAL] WRAPPER clause is not specified
Examples
The following query returns the context item, or the specified string of JSON data. The path expression
matches a single JSON object, which does not require an array wrapper. Note that the JSON data is converted
to strict JSON syntax in the returned value—that is, the object property names are enclosed in double quotation
marks.
FROM DUAL;
VALUE
------------------------------------------------------------------------------
--
{"a":100,"b":200,"c":300}
The following query returns the value of the member with property name a. The path expression matches a
scalar value, which must be enclosed in an array wrapper. Therefore, the WITH WRAPPER clause is specified.
FROM DUAL;
VALUE
------------------------------------------------------------------------------
--
[100]
The following query returns the values of all object members. The path expression matches multiple values,
which together must be enclosed in an array wrapper. Therefore, the WITH WRAPPER clause is specified.
FROM DUAL;
VALUE
------------------------------------------------------------------------------
--
[100,200,300]
The following query returns the context item, or the specified string of JSON data. The path expression
matches a single JSON array, which does not require an array wrapper.
FROM DUAL;
VALUE
------------------------------------------------------------------------------
--
[0,1,2,3,4]
The following query is similar to the previous query, except the WITH WRAPPER clause is specified. Therefore,
the JSON array is wrapped in an array wrapper.
FROM DUAL;
VALUE
------------------------------------------------------------------------------
--
[[0,1,2,3,4]]
The following query returns all elements in a JSON array. The path expression matches multiple values, which
together must be enclosed in an array wrapper. Therefore, the WITH WRAPPER clause is specified.
FROM DUAL;
VALUE
------------------------------------------------------------------------------
--
[0,1,2,3,4]
The following query returns the elements at indexes 0, 3 through 5, and 7 in a JSON array. The path expression
matches multiple values, which together must be enclosed in an array wrapper. Therefore,
the WITH WRAPPER clause is specified.
FROM DUAL;
VALUE
------------------------------------------------------------------------------
--
[0,3,4,5,7]
The following query returns the fourth element in a JSON array. The path expression matches a scalar value,
which must be enclosed in an array wrapper. Therefore, the WITH WRAPPER clause is specified.
FROM DUAL;
VALUE
------------------------------------------------------------------------------
--
[3]
The following query returns the first element in a JSON array. The WITH CONDITIONAL WRAPPER clause is
specified and the path expression matches a single JSON object. Therefore, the value returned is not wrapped
in an array. Note that the JSON data is converted to strict JSON syntax in the returned value—that is, the
object property name is enclosed in double quotation marks.
FROM DUAL;
VALUE
------------------------------------------------------------------------------
--
{"a":100}
FROM DUAL;
VALUE
------------------------------------------------------------------------------
--
[{"a":100},{"b":200},{"c":300}]
The following query is similar to the previous query, except that the value returned is of data
type VARCHAR2(100).
FROM DUAL;
VALUE
------------------------------------------------------------------------------
--
[{"a":100},{"b":200},{"c":300}]
The following query returns the fourth element in a JSON array. However, the supplied JSON array does not
contain a fourth element, which results in an error. The EMPTY ON ERROR clause is specified. Therefore, the
query returns an empty JSON array.
FROM DUAL;
VALUE
------------------------------------------------------------------------------
--
[]
JSON_TABLE
Note:
The JSON_TABLE function is available starting with Oracle Database 12c Release 1 (12.1.0.2).
Syntax
(JSON_path_expression::=, JSON_table_on_error_clause::=, JSON_columns_clause::=)
JSON_path_expression::=
object_step::=
array_step::=
Description of the illustration ''array_step.gif''
JSON_table_on_error_clause::=
JSON_columns_clause::=
JSON_column_definition::=
JSON_exists_column::=
Description of the illustration ''json_exists_column.gif''
JSON_query_column::=
JSON_value_column::=
JSON_nested_path::=
(JSON_path_expression::=, JSON_columns_clause::=)
ordinality_column::=
Purpose
JSON_TABLE creates a relational view of JSON data. It maps the result of a JSON data evaluation into
relational rows and columns. You can query the result returned by the function as a virtual relational table
using SQL. The main purpose of JSON_TABLE is to create a row of relational data for each object inside a
JSON array and output JSON values from within that object as individual SQL column values.
The COLUMNS clause enables you to search for JSON values in different ways by using the following clauses:
The column definition clauses allow you to specify a name for each column of data that they return. You can
reference these column names elsewhere in the SELECT statement, such as in the SELECT list and
the WHERE clause.
expr
Use this clause to specify the JSON data to be evaluated. For expr, specify an expression that evaluates to a
text literal. If expr is a column, then the column must be of data type VARCHAR2, CLOB, or BLOB. If expr is
null, then the function returns null.
If expr is not a text literal of well-formed JSON data using strict or lax syntax, then the function returns null
by default. You can use the JSON_table_on_error_clause to override this default behavior. Refer
to JSON_table_on_error_clause.
FORMAT JSON
JSON_path_expression
Use this clause to specify the row path expression. The function uses the row path expression to
evaluate expr and find the a JSON value, called the row source, that matches, or satisfy, the path expression.
This row source is then evaluated by the COLUMNS clause. The path expression must be a text literal.
JSON_table_on_error_clause
Use this clause to specify the value returned by this function when the following errors occur:
expr is not well-formed JSON data using strict or lax JSON syntax
No match is found when the JSON data is evaluated using the row path expression
JSON_columns_clause
Use the COLUMNS clause to define the columns in the virtual relational table returned by
the JSON_TABLE function.
You can use the JSON_value_return_type clause to control the data type of the returned column. If you
omit this clause, then the data type is VARCHAR2(4000). Use column_name to specify the name of the
returned column. The rest of the clauses of JSON_exists_column have the same semantics here as they
have for the JSON_EXISTS condition. For full information on these clauses, refer to "JSON_EXISTS
Condition". Also see "Using JSON_exists_column: Examples" for an example.
JSON_query_column This clause evaluates JSON data in the same manner as the JSON_QUERY function, that
is, it finds one or more specified JSON values, and returns a column of character strings that contain those
JSON values.
Use column_name to specify the name of the returned column. The rest of the clauses
of JSON_query_column have the same semantics here as they have for the JSON_QUERY function. For full
information on these clauses, refer to JSON_QUERY. Also see "Using JSON_query_column: Example" for an
example.
JSON_value_column This clause evaluates JSON data in the same manner as the JSON_VALUE function, that
is, it finds a specified scalar JSON value, and returns a column of those JSON values as SQL values.
Use column_name to specify the name of the returned column. The rest of the clauses
of JSON_value_column have the same semantics here as they have for the JSON_VALUE function. For full
information on these clauses, refer to JSON_VALUE. Also see "Using JSON_value_column: Example" for an
example.
JSON_nested_path Use this clause to flatten JSON values in a nested JSON object or JSON array into
individual columns in a single row along with JSON values from the parent object or array. You can use this
clause recursively to project data from multiple layers of nested objects or arrays into a single row.
Specify the JSON_path_expression clause to match the nested object or array. This path expression is
relative to the row path expression specified in the JSON_TABLE function.
Use the COLUMNS clause to define the columns of the nested object or array to be returned. This clause is
recursive—you can specify the JSON_nested_path clause within another JSON_nested_path clause.
Also see "Using JSON_nested_path: Examples" for an example.
ordinality_column This clause returns a column of generated row numbers of data type NUMBER. You can
specify at most one ordinality_column. Also see "Using JSON_value_column: Example" for an example
of using the ordinality_column clause.
Examples
Creating a Table That Contains a JSON Document: Example This example shows how to create and
populate table j_purchaseorder, which is used in the rest of the JSON_TABLE examples in this section.
The following statement creates table j_purchaseorder. Column po_document is for storing JSON data
and, therefore, has an IS JSON check constraint to ensure that only well-formed JSON is stored in the column.
The following statement inserts one row, or one JSON document, into table j_purchaseorder:
INSERT INTO j_purchaseorder
VALUES (
SYS_GUID(),
SYSTIMESTAMP,
'{"PONumber" : 1600,
"Reference" : "ABULL-20140421",
"User" : "ABULL",
"CostCenter" : "A50",
"state" : "CA",
"zipCode" : 99236,
"AllowPartialShipment" : true,
"LineItems" : [{"ItemNumber" : 1,
"UnitPrice" : 19.95,
"UPCCode" : 13131092899},
"Quantity" : 9.0},
{"ItemNumber" : 2,
"Part" : {"Description" : "Lethal Weapon",
"UnitPrice" : 19.95,
"UPCCode" : 85391628927},
"Quantity" : 5.0}]}');
Using JSON_query_column: Example The statement in this example queries JSON data for a specific JSON
property using the JSON_query_column clause, and returns the property value in a column.
The statement first applies a row path expression to column po_document, which results in a match to
the ShippingInstructions property. The COLUMNS clause then uses the JSON_query_column clause
to return the Phone property value in a VARCHAR2(100) column.
SELECT jt.phones
FROM j_purchaseorder,
JSON_TABLE(po_document, '$.ShippingInstructions'
COLUMNS
PHONES
------------------------------------------------------------------------------
-------
[{"type":"Office","number":"909-555-7307"},{"type":"Mobile","number":"415-555-
1234"}]
Using JSON_value_column: Example The statement in this example refines the statement in the previous
example by querying JSON data for specific JSON values using the JSON_value_column clause, and
returns the JSON values as SQL values in relational rows and columns.
The statement first applies a row path expression to column po_document, which results in a match to the
elements in the JSON array Phone. These elements are JSON objects that contain two members
named type and number. The statement uses the COLUMNS clause to return the type value for each object in
a VARCHAR2(10) column called phone_type, and the number value for each object in
a VARCHAR2(20) column called phone_num. The statement also returns an ordinal column
named row_number.
SELECT jt.*
FROM j_purchaseorder,
JSON_TABLE(po_document, '$.ShippingInstructions.Phone[*]'
COLUMNS (row_number FOR ORDINALITY,
AS jt;
1 Office 909-555-7307
2 Mobile 415-555-1234
Using JSON_exists_column: Examples The statements in this example test whether a JSON value exists in
JSON data using the JSON_exists_column clause. The first example returns the result of the test as a
'true' or 'false' value in a column. The second example uses the result of the test in the WHERE clause.
The following statement first applies a row path expression to column po_document, which results in a
match to the entire context item, or JSON document. It then uses the COLUMNS clause to return the requestor's
name and a string value of 'true' or 'false' indicating whether the JSON data for that requestor contains a
zip code. The COLUMNS clause first uses the JSON_value_column clause to return the Requestor value in
a VARCHAR2(32) column called requestor. It then uses the JSON_exists_column clause to determine
if the zipCode object exists and returns the result in a VARCHAR2(5) column called has_zip.
FROM j_purchaseorder,
JSON_TABLE(po_document, '$'
COLUMNS
REQUESTOR HAS_ZIP
-------------------------------- -------
The following statement is similar to the previous statement, except that it uses the value of has_zip in
the WHERE clause to determine whether to return the Requestor value:
SELECT requestor
FROM j_purchaseorder,
JSON_TABLE(po_document, '$'
COLUMNS
REQUESTOR
--------------------------------
Alexis Bull
The following statement does not use the JSON_nested_path clause. It returns the three elements in the
array in a single row. The nested array is returned in its entirety.
SELECT *
1 2 ["a","b"]
The following statement is different from the previous statement because it uses
the JSON_nested_path clause to return the individual elements of the nested array in individual columns in
a single row along with the parent array elements.
SELECT *
1 2 a b
The previous example shows how to use JSON_nested_path with a nested JSON array. The following
example shows how to use the JSON_nested_path clause with a nested JSON object by returning the
individual elements of the nested object in individual columns in a single row along with the parent object
elements.
SELECT *
SELECT jt.*
FROM j_purchaseorder,
JSON_TABLE(po_document, '$'
COLUMNS
AS jt;
JSON_VALUE
Note:
The JSON_VALUE function is available starting with Oracle Database 12c Release 1 (12.1.0.2).
Syntax
object_step::=
array_step::=
JSON_value_returning_clause::=
JSON_value_return_type::=
Description of the illustration ''json_value_return_type.gif''
JSON_value_on_error_clause::=
Purpose
JSON_VALUE finds a specified scalar JSON value in JSON data and returns it as a SQL value.
expr
Use this clause to specify the JSON data to be evaluated. For expr, specify an expression that evaluates to a
text literal. If expr is a column, then the column must be of data type VARCHAR2, CLOB, or BLOB. If expr is
null, then the function returns null.
If expr is not a text literal of well-formed JSON data using strict or lax syntax, then the function returns null
by default. You can use the JSON_value_on_error_clause to override this default behavior. Refer to
the JSON_value_on_error_clause.
FORMAT JSON
JSON_path_expression
Use this clause to specify a JSON path expression. The function uses the path expression to evaluate expr and
find a scalar JSON value that matches, or satisfies, the path expression. The path expression must be a text
literal.
The path expression must begin with a dollar sign ($), which represents the context item, that is, the expression
specified by expr. The dollar sign is followed by zero or more steps, each of which can be an object step or an
array step.
The function attempts to match the first step in the path expression to the context item. If the first step results
in a match, then the function attempts to match the second step to the JSON value(s) that matched the first
step. If the second step results in a match, then the function attempts to match the third step to the JSON
value(s) that matched the second step, and so on. If the final step matches a scalar JSON value, then the
function returns that value as a SQL value. A path expression that consists of a dollar sign followed by zero
steps ('$') matches the entire context item.
You can specify the JSON_value_returning_clause to control the data type and format of the returned
SQL value. Refer to the JSON_value_returning_clause.
If any step in the path expression does not result in a match, or if the final step matches a nonscalar value, then
the function returns null by default. You can use the JSON_value_on_error_clause to override this default
behavior. Refer to the JSON_value_on_error_clause.
A simple_name can contain only alphanumeric characters and must begin with an alphabetic
character. A complex_name can contain only alphanumeric characters and spaces, and must begin
with an alphanumeric character. A complex_name must be enclosed in double quotation marks.
Use the asterisk wildcard symbol (*) to specify all property names. If the JSON object being evaluated
contains at least one member, then the object step results in a match to the values of all members.
Otherwise, the object step does not result in a match.
If you apply an object step to a JSON array, then the array is implicitly unwrapped and the elements of the
array are evaluated using the object step. This is called JSON path expression relaxation. Refer to Oracle XML
DB Developer's Guide for more information.
If the JSON value being evaluated is not a JSON object, then the object step does not result in a match.
JSON_value_returning_clause
Use this clause to specify the data type and format of the value returned by this function.
RETURNING Use the RETURNING clause to specify the data type of the return value. If you omit this clause,
then JSON_VALUE returns a value of type VARCHAR2(4000).
VARCHAR2[(size [BYTE,CHAR])]
If you specify this data type, then the scalar value returned by this function can be a character or
number value. A number value will be implicitly converted to a VARCHAR2. When specifying
the VARCHAR2 data type elsewhere in SQL, you are required to specify a size. However, in this clause
you can omit the size. In this case, JSON_VALUE returns a value of type VARCHAR2(4000).
NUMBER[(precision [, scale])]
If you specify this data type, then the scalar value returned by this function must be a number value.
If the data type is not large enough to hold the return value, then this function returns null by default. You can
use the JSON_value_on_error_clause to override this default behavior. Refer to
the JSON_value_on_error_clause.
ASCII Specify ASCII to automatically escape any non-ASCII Unicode characters in the return value, using
standard ASCII Unicode escape sequences.
JSON_value_on_error_clause
Use this clause to specify the value returned by this function when any of the following errors occur:
expr is not well-formed JSON data using strict or lax JSON syntax
A nonscalar value or no match is found when the JSON data is evaluated using the JSON path
expression
The return value data type is not large enough to hold the return value
Examples
The following query returns the value of the member with property name a. Because the RETURNING clause is
not specified, the value is returned as a VARCHAR2(4000) data type:
FROM DUAL;
VALUE
-----
100
The following query returns the value of the member with property name a. Because
the RETURNING NUMBER clause is specified, the value is returned as a NUMBER data type:
FROM DUAL;
VALUE
----------
100
The following query returns the value of the member with property name b, which is in the value of the
member with property name a:
FROM DUAL;
VALUE
-----
100
The following query returns the value of the member with property name d in any object:
FROM DUAL;
VALUE
-----
200
The following query returns the value of the first element in an array:
FROM DUAL;
VALUE
-----
The following query returns the value of the third element in an array. The array is the value of the member
with property name a.
FROM DUAL;
VALUE
-----
15
The following query returns the value of the member with property name a in the second object in an array:
FROM DUAL;
VALUE
-----
200
The following query returns the value of the member with property name c in any object in an array:
FROM DUAL;
VALUE
-----
300
The following query attempts to return the value of the member that has property name lastname. However,
such a member does not exist in the specified JSON data, resulting in no match. Because the ON ERROR clause
is not specified, the statement uses the default NULL ON ERROR and returns null.
FROM DUAL;
Last Name
---------
The following query results in an error because it attempts to return the value of the member with property
name lastname, which does not exist in the specified JSON. Because the ON ERROR clause is specified, the
statement returns the specified text literal.
FROM DUAL;
Last Name
---------
LAG
Syntax
See Also:
"Analytic Functions" for information on syntax, semantics, and restrictions, including valid forms
of value_expr
Purpose
LAG is an analytic function. It provides access to more than one row of a table at the same time without a self
join. Given a series of rows returned from a query and a position of the cursor, LAG provides access to a row at
a given physical offset prior to that position.
For the optional offset argument, specify an integer that is greater than zero. If you do not specify offset,
then its default is 1. The optional default value is returned if the offset goes beyond the scope of the
window. If you do not specify default, then its default is null.
You cannot nest analytic functions by using LAG or any other analytic function for value_expr. However,
you can use other built-in function expressions for value_expr.
See Also:
"About SQL Expressions" for information on valid forms of expr and LEAD
Examples
The following example provides, for each purchasing clerk in the employees table, the salary of the
employee hired just before:
FROM employees
ORDER BY hire_date;
LAST
Syntax
last::=
Description of the illustration ''last.gif''
See Also:
"Analytic Functions" for information on syntax, semantics, and restrictions of
the query_partitioning_clause
Purpose
FIRST and LAST are very similar functions. Both are aggregate and analytic functions that operate on a set of
values from a set of rows that rank as the FIRST or LAST with respect to a given sorting specification. If only
one row ranks as FIRST or LAST, then the aggregate operates on the set with only one element.
Refer to FIRST for complete information on this function and for examples of its use.