NUM_ROWS in DBA_TABLES not reflected upon metadata sharing - oracle

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.

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

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.

In sql without insert values in table, how i can see column of table

SQL> create table justlike(customerid varchar(19),first_name varchar(40),last_name varchar(100),Address varchar(50),city varchar(30),pincode varchar(10),state varchar(20));
Just use:
desc <table_name>;
This will print the description of your table columns
in your case:
desc justlike;
You can always check the table definition, in case you are using Oracle, by running below query -
SELECT * FROM USER_TAB_COLS
WHERE TABLE_NAME = 'JUSTLIKE';
OR you can write a select on table itself -
SELECT * FROM JUSTLIKE;

Table tablespace shown as X but appears to be 'Users'

I am using SQL*Plus to create a table with the following sql:
CREATE TABLE SCHEMA_OWNER.TEST_TABLE (
ID NUMBER(19,0) NOT NULL,
TEST_STRING VARCHAR2(255 CHAR),
PRIMARY KEY (ID)
);
When I try and do an insert with any user into that table, I see the following error:
INSERT INTO TEST_TABLE (ID, TEST_STRING) values (1, 'Test')
ORA-01950: no privileges on tablespace 'USERS'
This implies that the table has been created in the Users tablespace, but when I look in the USER_TABLES table, I see that the tablespace is the same as all of the other tables, and not 'Users':
SELECT table_name, tablespace_name from USER_TABLES;
TABLE_NAME TABLESPACE_NAME
---------------- ---------------
TABLE1 DATA_TABLESPACE
TEST_TABLE DATA_TABLESPACE
When I create the table in SQL developer using the same SQL, I don't have any errors when inserting data.
1) Why am I getting this error when the table isn't in the Users tablespace?
2) Why am I only getting it when running the sql script from SQL*Plus?
Thanks to Aleksej for suggesting it could be an issue with the indexes. The problem was that when I was creating the index on the table, I wasn't creating it for the schema owner, which meant that it was being created in a different tablespace.
So instead of
CREATE INDEX TEST_TABLE_IDX ON SCHEMA_OWNER.TEST_TABLE(TEST_STRING);
I needed to do
CREATE INDEX SCHEMA_OWNER.TEST_TABLE_IDX ON SCHEMA_OWNER.TEST_TABLE(TEST_STRING);

Incorrect output while using dictionary tables inside Trigger

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

Resources