This is an attempt to the test the usage of the VIRTUAL column with a function to increment the value in a column.
I am using a function which would return the last two digits of the current year, concatenated with a hyphen followed by the next max value from the table column which is defined as a virtual column.
When I insert records into the table, it does insert successfully. However, when I am querying records, I am getting the below error
ORA-00036: maximum number of recursive SQL levels (50) exceeded
My question is, does it possible to achieve to increment the values (custom increment with the using a VIRTUAL column or this attempt is trivial?
The below function is compiled first by uncommenting the commented part and upon the creation of the table, the first SQL block is commented and I use the second SQL block
Function
CREATE OR REPLACE FUNCTION test_func (
p_empl_id NUMBER,
empl_nm VARCHAR2)
RETURN VARCHAR2
DETERMINISTIC
IS
return_value VARCHAR2(32);
BEGIN
return_value := NULL;
-- SELECT TO_CHAR (SYSDATE, 'YY')
-- || '-'
-- || LPAD (
-- TO_CHAR (NVL (MAX (TO_NUMBER (SUBSTR (001, 5))), 0) + 1),
-- 5,
-- '0') into return_value
-- FROM dual;
SELECT TO_CHAR (SYSDATE, 'YY')
|| '-'
|| LPAD (
TO_CHAR (NVL (MAX (TO_NUMBER (SUBSTR (test_col, 5))), 0) + 1),
5,
'0')
INTO return_value
FROM test_table
WHERE SUBSTR (test_col, 1, 2) = TO_CHAR (SYSDATE, 'YY');
RETURN return_value;
END;
/
Table Structure
CREATE TABLE test_table
(
empl_id NUMBER,
empl_nm VARCHAR2 (50),
monthly_sal NUMBER (10, 2),
bonus NUMBER (10, 2),
test_col AS (test_func (empl_id, empl_nm)) VIRTUAL
);
Insert Statement
INSERT INTO test_table (empl_id,
empl_nm,
monthly_sal,
bonus)
WITH data
AS (SELECT 100 empl_id,
'AAA' empl_nm,
20000 monthly_sal,
3000 bonus
FROM DUAL)
SELECT *
FROM data;
I have tried using the below SQL using a sequence, however, the sequence value is getting inserted every time I execute a SQL statement from the table
SELECT TO_CHAR (SYSDATE, 'YY')
|| '-'
|| '000'
|| test_virtual_sequence.NEXTVAL
FROM DUAL;