1. Regular Expressions
2. Merge Statement Enhancements
3. Collect Function
4. DBMS_OUTPUT Size Limit
5. Quoting String Literals
1. Regular Expressions
A regular expression is a set of characters that specify a search pattern.
Oracle 10g provides 4 functions to implement regular expressions.
- REGEXP_LIKE - Returns true if the pattern is matched, otherwise false.
- REGEXP_INSTR - Returns the position of the start or end of the matching string. Returns zero if the pattern does not match.
- REGEXP_REPLACE - Returns a string where each matching string is replaced with the text specified.
- REGEXP_SUBSTR - Returns the matching string, or NULL if no match is found.
We will discuss more on Regular Expressions in coming posts...
2. Merge Statement Enhancements
Sample Merge Statement in 9i
MERGE INTO table_name tbl
USING (SELECT NULL FROM DUAL)
ON (tbl.column1 = l_value1 AND
tbl.column2 = l_value2
)
WHEN MATCHED THEN
UPDATE SET column3 = l_value3
,column4 = l_value4
WHEN NOT MATCHED THEN
INSERT ( column1
,column2
,column3
,column4
)
VALUES ( l_value1
,l_value2
,l_value3
,l_value4
);
Enhancements in 10g:
- The MATCHED and NOT MATCHED clauses are now optional
--Merge statement with only MATCHED clause
MERGE INTO table_name tbl
USING (SELECT NULL FROM DUAL)
ON (tbl.column1 = l_value1 AND
tbl.column2 = l_value2
)
WHEN MATCHED THEN
UPDATE SET column3 = l_value3
,column4 = l_value4;
--Merge statement with only NOT MATCHED clause
MERGE INTO table_name tbl
USING (SELECT NULL FROM DUAL)
ON (tbl.column1 = l_value1 AND
tbl.column2 = l_value2
)
WHEN NOT MATCHED THEN
INSERT ( column1
,column2
,column3
,column4
)
VALUES ( l_value1
,l_value2
,l_value3
,l_value4
);
- Conditional inserts and updates are now possible by using a WHERE clause
MERGE INTO table_name tbl
USING (SELECT NULL FROM DUAL)
ON (tbl.column1 = l_value1 AND
tbl.column2 = l_value2
)
WHEN MATCHED THEN
UPDATE SET column3 = l_value3
,column4 = l_value4
WHERE column5 = l_value5
WHEN NOT MATCHED THEN
INSERT ( column1
,column2
,column3
,column4
)
VALUES ( l_value1
,l_value2
,l_value3
,l_value4
)
WHERE l_value5 <> 100;
- An optional DELETE WHERE clause can be used to clean up after a merge operation. Only those rows which match both the ON clause and the DELETE WHERE clause are deleted.
MERGE INTO table_name tbl
USING (SELECT NULL FROM DUAL)
ON (tbl.column1 = l_value1 AND
tbl.column2 = l_value2
)
WHEN MATCHED THEN
UPDATE SET column3 = l_value3
,column4 = l_value4
WHERE column5 = l_value5
WHEN NOT MATCHED THEN
INSERT ( column1
,column2
,column3
,column4
)
VALUES ( l_value1
,l_value2
,l_value3
,l_value4
)
WHERE l_value5 <> 100
DELETE WHERE (column4 <> l_value4);
3. Collect Function
COLLECT function is used to aggregate data into a collection, retaining multiple records of data within a single row (like a nested table).
SQL> SELECT manager_name, COLLECT(emp_name) Emps
2 FROM test_employee
3 GROUP BY manager_name;
MANAGER_NAME EMPS
-----------------------------------------------------------------------
Reddy SYSTPgYIGt3FzukLgQGRALxhZSQ==('Gagan', 'Surya')
Vijay SYSTPgYIGt3FzukLgQGRALxhZSQ==('Akash', 'Ganga', 'Prithvi')
Here SYSTPgYIGt3FzukLgQGRALxhZSQ== is a supporting type (Collection) created by Oracle.
4. DBMS_OUTPUT Size Limit
Prior to Oracle 10gR2, put and put_line procedures of dbms_output package have an imposed limit of 255 bytes. In 10.2, this restriction has been relaxed to 32,767 bytes.
5. Quoting String Literals
Oracle 10g introduces new quoting mechanism in PL/SQL which enables us to embed single-quotes in literal strings without having to resort to double, triple or sometimes quadruple quote characters. It is very useful in writing dynamic queries.
The quoting mechanism is invoked with a simple "q" in PL/SQL. The syntax is q'[...]', where the "[" and "]" characters can be any of the following.
! !
[ ]
{ }
( )
< >
DECLARE
l_sql VARCHAR2(1024);
l_cnt PLS_INTEGER;
BEGIN
l_sql := q'{SELECT COUNT(emp_name)
FROM test_employee
WHERE manager_name = 'Vijay'
}';
EXECUTE IMMEDIATE l_sql INTO l_cnt;
DBMS_OUTPUT.PUT_LINE(l_cnt || ' employees reporting to Vijay.');
END;
Output:
3 employees reporting to Vijay.
More to follow...