Incorrect output while using dictionary tables inside Trigger - oracle

I am using ALL_TABLES/ALL_TAB_COLUMNS to get count of tables in my schema (EDW_SRC) and another schema(EDW_STG). I get correct counts when i run the query in my sql developer as shown below. But if i put the same query inside a trigger, i get wrong count for other schema(EDW_STG).
Please refer below code:
(This is just a sample code to replicate the issue, not my business requirement. I am referring ALL_TAB_COLUMNS in my actual code to get the number of columns in a particular table in different schema, for which i have Select access.)
select user from dual;
USER
-----
EDW_SRC
DROP TABLE ABC;
Table ABC dropped.
CREATE TABLE ABC(ID NUMBER);
Table ABC created.
select count(1) EDW_STG_CNT
from all_tables
where owner='EDW_STG';--Different Schema
EDW_STG_CNT
----------
101
select count(1) EDW_SRC_CNT
from all_tables
where owner='EDW_SRC';--My Schema
EDW_SRC_CNT
------------
1554
create or replace trigger trig_test_dml_abc
before insert on abc
DECLARE
V_STG_CNT number :=NULL;
V_SRC_CNT number :=NULL;
begin
DBMS_OUTPUT.PUT_LINE('***** TRIGGER OUTPUT *****');
select count(1) into V_SRC_CNT from all_tables
where owner='EDW_SRC'; --My Schema
DBMS_OUTPUT.PUT_LINE('My Schema EDW_SRC_CNT :'||V_SRC_CNT);
select count(1) into V_STG_CNT from all_tables
where owner='EDW_STG'; --Different Schema
DBMS_OUTPUT.PUT_LINE('Different Schema EDW_STG_CNT :'||V_STG_CNT);
end;
Trigger TRIG_TEST_DML_ABC compiled
INSERT INTO ABC VALUES (2);
1 row inserted.
***** TRIGGER OUTPUT *****
My Schema EDW_SRC_CNT :1554
Different Schema EDW_STG_CNT :2
The Different Schema count should be 101. Why is it coming as 2.
Oracle Version:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
Thanks
K

Related

How to view the contents of a nested table column in SQL developer?

I am using Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit
and SQL Developer version Version 21.4.1.349
Build 349.1822
I have a schema level type as follows
CREATE OR REPLACE TYPE CHAR_TAB_20_TYPE IS TABLE OF VARCHAR2 (20);
The above type is the datatype of the column order_consignee_tab in the table table_1.
When I execute the below select statement i get the output in SQL Developer
as follows which is not displaying the values in the nested table.
SELECT selection_id,
order_consignee_tab
FROM table_1
WHERE col_1 = '206' AND selection_id = 'TEST';
Output:
SELECTION_ID ORDER_CONSIGNEE_TAB
TEST WMSPRD.CHAR_TAB_20_TYPE()
Can somebody please tell me how to view the values of the nested table column order_consignee_tab.
As you asked about SQL Developer, then follow steps on this screenshot:
double-click the result of the select statement
click the pencil icon
view the result
You can JOIN to a table collection expression and then get the value from the COLUMN_VALUE pseudo-column:
SELECT selection_id,
o.COLUMN_VALUE AS order_consignee
FROM table_1 t
CROSS JOIN TABLE(t.order_consignee_tab) o
WHERE col_1 = '206'
AND selection_id = 'TEST';
Which, for the sample data:
CREATE TABLE table_1 (
selection_id VARCHAR2(4),
col_1 VARCHAR2(3),
order_consignee_tab CHAR_TAB_20_TYPE
) NESTED TABLE order_consignee_tab STORE AS table_1__order_consignee_tab;
INSERT INTO table_1 (selection_id, col_1, order_consignee_tab)
VALUES ('TEST', '206', CHAR_TAB_20_TYPE('ABC', 'DEF', 'GHI'));
Outputs:
SELECTION_ID
ORDER_CONSIGNEE
TEST
ABC
TEST
DEF
TEST
GHI
db<>fiddle here

adding a sequence to an existing table

i created a table but i forgot to add a sequence to one of the PK, its a sequence on a form page, i just cant find anything about it, is it possible or do i have to do the form all over again.
i tried to replace the PK but it doesnt give me the option to add the sequence when creating a new one.
i searched everywhere and asked the support in chat (didn't really help since its not their job).
all i could find was this and this.
I'd suggest you to skip Apex in this matter and do the following: presume this is your table:
SQL> create table test
2 (id number constraint pk_test primary key,
3 name varchar2(20)
4 );
Table created.
This is the sequence:
SQL> create sequence myseq;
Sequence created.
As you forgot to specify PK source while creating Apex Form page, never mind - let the database handle it. How? Create a BEFORE INSERT trigger:
SQL> create or replace trigger trg_bi_test
2 before insert on test
3 for each row
4 when (new.id is null)
5 begin
6 :new.id := myseq.nextval;
7 end trg_bi_test;
8 /
Trigger created.
Let's test it: I'm inserting only the NAME (which is what your Apex Form will be doing):
SQL> insert into test (name) values ('Littlefoot');
1 row created.
What is table's contents?
SQL> select * from test;
ID NAME
---------- --------------------
1 Littlefoot
SQL>
See? Trigger automatically inserted ID (primary key) column value.
If it were an Interactive Grid (which lets you insert several records at a time):
SQL> insert into test (name)
2 select 'Bigfoot' from dual union all
3 select 'FAD' from dual;
2 rows created.
SQL> select * from test;
ID NAME
---------- --------------------
1 Littlefoot
2 Bigfoot
3 FAD
SQL>
Works just fine.
And what's another benefit: you don't have to modify Apex application at all.

select from the table name values after _ using oracle sql

Suppose if the table name is ABC_XYZ_123. I want to extract the integer values after _.
The output should be integer values after _.
In the above case, the output should be 123.
I have used the below sql query.
select from table_name like 'XXX_%';
But I am not getting required output. Can anyone help me with this query.
Thanks
Using REGEXP_SUBSTR with a capture group we can try:
SELECT REGEXP_SUBSTR(name, '_(\d+)$', 1, 1, NULL, 1)
FROM yourTable;
The question is somewhat unclear:
it looks as if you're looking for table names that contain number at the end, while
query you posted suggests that you're trying to select those numbers from one of table's columns
I'll stick to
Suppose if the table name is ABC_XYZ_123
If that's so, it is the data dictionary you'll query. USER_TABLES contains that information.
Let's create that table:
SQL> create table abc_xyz_123 (id number);
Table created.
Query selects numbers at the end of table names, for all my tables that end with numbers.
SQL> select table_name,
2 regexp_substr(table_name, '\d+$') result
3 from user_tables
4 where regexp_like(table_name, '\d+$');
TABLE_NAME RESULT
-------------------- ----------
TABLE1 1
TABLE2 2
restore_point-001 001
ABC_XYZ_123 123 --> here's your table
SQL>
Apparently, I have a few of them.

NUM_ROWS in DBA_TABLES not reflected upon metadata sharing

In Oracle 12c, I have a table created with sharing = metadata. Following are the sql statements:
create table fedcommusr.md_commtab1 sharing=metadata
(deptno number, dname varchar2(100));
insert into fedcommusr.md_commtab1 values (1, 'One');
insert into fedcommusr.md_commtab1 values (2, 'Two');
comment on column fedcommusr.md_commtab1.deptno is 'department number';
comment on column fedcommusr.md_commtab1.dname is 'Department name is';
Executed the DBMS_STATS as follows:
exec DBMS_STATS.GATHER_SCHEMA_STATS(ownname=>'FEDCOMMUSR');
Following is the query executed to obtain the num_rows
select owner,table_name, NUM_ROWS from dba_tables where owner like upper('%fed%') ;
and output is as follows:
FEDCOMMUSR MD_COMMTAB1 (null)
Why are the num_rows not updated ?
In 12.2 latest RU I tested and have no problem: stats gathered and visible on application root as well as application PDB.
You can trace statistics gathering with dbms_stats.set_global_prefs('trace',1+4) and set serveroutput on to show it.
Regards,
Franck.

¿Trigger in oracle after insert?

I have a netbeans web application using oracle database 11g, and i need to insert data on my table_B automatically after instert on my table_A,
my table_A has:
id_product
del_product
contrat
pre_prod
and my table_B has the same columns but i need to copy all data from table_A after update.
i'm new using triggeres and i don't know anythoing about it, Could you help me?
this the code i try but it doesn't work.
create or replace trigger color_omes
after update update of (id_del, dele, contr)
on dele
for each row
begin
insert into del3 values(new.id_del, new.dele, new.contr);
end color_omes;
but something went wrong, i get this error
ORA-00969: missing ON keyword
You have some issues in your code:
CREATE OR REPLACE TRIGGER color_omes
AFTER UPDATE OF id_del, dele, contr
ON dele
FOR EACH ROW
BEGIN
INSERT INTO del3 VALUES(:new.id_del, :new.dele, :new.contr);
END color_omes;
How it works:
SQL> select * from del3;
no rows selected
SQL> update dele set contr = 100;
1 row updated.
SQL> select * from del3;
ID_DEL DELE CONTR
---------- ---------- ----------
1 2 100

Resources