Unable to select recent table name from all_tables table - oracle

I have a table which contains details of all the tables present in a particular schema. The name of the table is "all_tables". There is a column called table_name which contains the name of all tables.
I have a table named "SBC_RAO_INDEX_30_05_2016_04_99". Here "30_05_2016_04_99" is date with time stamp.
Like wise I have tables with different time stamps as shown below.
SBC_RAO_INDEX_30_05_2016_04_99,
SBC_RAO_INDEX_31_05_2016_04_99,
SBC_RAO_INDEX_01_06_2016_04_99,
SBC_RAO_INDEX_02_06_2016_04_99,
SBC_RAO_INDEX_03_06_2016_04_99
I am trying to fetch the latest table name from all_tables using below query:
select *
from all_tables
WHERE table_name like 'SBC_RAO_INDEX%'
and table_name not like 'SBC_RAO_INDEX_BKP%'
order by table_name desc
But this query is giving me SBC_RAO_INDEX_31_05_2016_04_99 as the first row which is not correct.
Is there any way to fetch the recent table name? Kindly help me in solving this

Try this. Hope it helps.
SELECT
A.*
FROM
(
SELECT
'SBC_RAO_INDEX_30_05_2016_04_99' AS fle
FROM
DUAL
UNION ALL
SELECT
'SBC_RAO_INDEX_31_05_2016_04_99' AS fle
FROM
DUAL
UNION ALL
SELECT
'SBC_RAO_INDEX_01_06_2016_04_99' AS fle
FROM
DUAL
UNION ALL
SELECT
'SBC_RAO_INDEX_02_06_2016_04_99' AS fle
FROM
DUAL
UNION ALL
SELECT
'SBC_RAO_INDEX_03_06_2016_04_99' AS FLE
FROM
DUAL
)
A
ORDER BY
TO_DATE(REPLACE(SUBSTR(A.FLE,15,10),'_','/'),'DD/MM/YYYY') DESC;

because in LIKE the '_' is a special character,
use SUBSTR instead:
SELECT *
FROM all_tables
WHERE SUBSTR(table_name,1,13) ='SBC_RAO_INDEX'
AND SUBSTR(table_name,15,3) <> 'BKP'
ORDER BY table_name DESC

Since the underscore is a special character to LIKE and it matches any single character, escape it to match a literal underscore by defining an escape character like this (you need the ESCAPE clause for each LIKE) and then place that character immediately before the underscore:
select *
from all_tables
WHERE table_name like 'SBC\_RAO\_INDEX%' ESCAPE '\'
and table_name not like 'SBC\_RAO\_INDEX\_BKP%' ESCAPE '\'
order by table_name desc;
Note you can still use the underscore to match a single character in the same string, just escape it when you need to match a literal underscore.

Thanks for your answers.
The below query fullfilled my requirement.
select table_name from all_tables WHERE
table_name like 'SBC_RAO_INDEX_%'
and table_name not like 'SBC_RAO_INDEX_BKP%'
order by TO_DATE(SUBSTR(table_name,15,10),'DD/MM/YYYY') DESC;

Related

first alias works second doesn't throws invalid identifier [duplicate]

How to 'group by' a query using an alias, for example:
select count(*), (select * from....) as alias_column
from table
group by alias_column
I get 'alias_column' : INVALID_IDENTIFIER error message. Why? How to group this query?
select
count(count_col),
alias_column
from
(
select
count_col,
(select value from....) as alias_column
from
table
) as inline
group by
alias_column
Grouping normally works if you repeat the respective expression in the GROUP BY clause. Just mentioning an alias is not possible, because the SELECT step is the last step to happen the execution of a query, grouping happens earlier, when alias names are not yet defined.
To GROUP BY the result of a sub-query, you will have to take a little detour and use an nested query, as indicated above.
Nest the query with the alias column:
select count(*), alias_column
from
( select empno, (select deptno from emp where emp.empno = e.empno) as alias_column
from emp e
)
group by alias_column;
select count(*), (select * from....) as alias_column
from table
group by (select * from....)
In Oracle you cannot use an alias in a group by clause.
To use an alias in Oracle you need to ensure that the alias has been defined by your query at the point at which the alias is being used.
The most straightforward way to do this is to simply treat the original query as a subquery -- in this case,
select count(*), (select * from....) as alias_column
from table
group by (select * from....)
becomes
select count, alias_column
from
(select count(*) as count, (select * from....) as alias_column
from table)
group by alias_column
I can't speak to the performance implications, but it's very quick to write if you're trying to re-use an alias in your query - throw everything in parentheses and jump up a level...
If you don't have to use an alias you could do it this way:
select
EXTRACT(year from CURRENT_DATE), count(*) from something
group by EXTRACT(year from CURRENT_DATE)
order by EXTRACT(year from CURRENT_DATE)
Instead of using alias and subquery.

How do I get all table names and its column names in oracle?

How do I get all table names and its column names in oracle? The Table names should be printed first followed by all the column name, then the next table and its columns, and so on and so forth.
If you are dba you can see all tables in DB;
select * from all_tab_columns
order by table_name;
The query below, in the middle column (str), has the data the way you requested it. However, without the info in the first and/or the last column, it is not clear how you will know which values are table names and which are columns under them.
It would make a lot more sense to have table name in one column (repeated for each of its columns), then the column name and then the order. This would be just the second member of the union all operation.
The query below works for the tables in your schema. If you want to do this for all the tables you have access to, use all_tables and all_tab_columns; if you have DBA privileges, use dba_tables and dba_tab_columns (but in these cases, don't you need to know the schema/owner, not just the table name?)
select table_name as tbl, table_name as str, 0 as ord from user_tables
union all
select table_name, column_name, column_id from user_tab_columns
order by tbl, ord
;
SELECT table_name, column_name
FROM all_tab_cols
order by table_name, column_name

What are the ways to write a sql script for getting mismatch between source and target datatype

Currently I am using
(select 'S',DATA_TYPE,DATA_LENGTH from all_tab_columns where table_name like '&Source_Table' and column_name='&Source_Column'
minus
select 'S',DATA_TYPE,DATA_LENGTH from all_tab_columns where table_name like '&Target_Table' and column_name='&Target_Column')
UNION ALL
(
select 'S',DATA_TYPE,DATA_LENGTH from all_tab_columns where table_name like '&Target_Table' and column_name='&Target_Column'
minus
select 'S',DATA_TYPE,DATA_LENGTH from all_tab_columns where table_name like '&Source_Table' and column_name='&Source_Column')
But above query need to run for each and every table. So i want to write a script which will use table and column names from a file/one time entry which will fetch mismatches between source and target.

How to search a column name within all tables of a database at specific schema

select table_name, column_name
from all_tab_columns
where column_name = '<YOUR_COLUMN_HERE_IN_CAPITAL_CASE>'
How can I use the above columns search query for a specific schema at Oracle DB?
I am asking because I have tried the query and it also returned many tables outside of the schema I am interested into, so they are of no interest to me.
select table_name, column_name
FROM all_tab_columns
where column_name = '<YOUR_COLUMN_HERE_IN_CAPITAL_CASE>'
and owner = '<schema>';
all_tab_columns contains all the columns on which the current user has privileges. So it may not return all the data.
dba_tab_columns contains information about all columns, but you may need some special privileges to query this dictionary view.
And finally, if you're interested only in the columns of all tables owned by the current user you can use:
select table_name, column_name
FROM user_tab_columns
where column_name = '<YOUR_COLUMN_HERE_IN_CAPITAL_CASE>';
But this view doesn't have an OWNER column (it only contains all the columns owned by the current user)
Use the following.
You can try this via SQL tool that is used by you
select table_name from all_tab_columns where column_name = 'PICK_COLUMN';
Or if you have DBA privileges,
select table_name from dba_tab_columns where column_name = 'PICK_COLUMN';
But if you are not sure about the column names you can add LIKE statements to current query.
Eg:
select table_name from all_tab_columns where column_name LIKE '%PICK_COLUMN%';
You can use the LIKE function to refine a search.
I don't know the exact names of the columns, but you could add the owner and the table name to this one:
select table_name, column_name
from all_tab_columns
where column_name = '<YOUR_COLUMN_HERE_IN_CAPITAL_CASE>'
in a way similar to this:
select column1,column2,column3,more_columns
from all_tables_you_need
where column1 like '%&column1%'
and column2 like '%&column2%'
and column3 like '%&column3%';
Either of those 3 variables can also be empty, but you can also put a very specific one in there, so that the result will vary a lot. Behold use of the UPPER function, if you are not sure of the case, or you want to ignore case sensitivity.

ORA-00918: column ambiguously defined: how to find the column

I'm getting the classic error:
ORA-00918: column ambiguously defined
Usually, I know how to solve it but my problem now is that I'm working with a 700 row query.
Is there a way to identify the column?
Have you tried to do a binary search?
e.g.
If your original query looks like
Select col1
,col2
,col3
,col4
from MyTable
you can start with commenting the 2nd half
Select col1
,col2
/*,col3
,col4 */
from MyTable
If you still get the error, run the query again commenting some column from the other half:
Select col1
/*,col2 */
,col3
,col4
from MyTable
If you still get an error then your problem is with col1, otherwise you need to change col2.
The ambiguous column error message indicates that you have joined two (or more) columns in your query which share the same column name.
The proper way to solve this is to give each table in the query an alias and then prefix all column references with the appropriate alias. I agree that won't be fun for such a large query but I'm afraid you will have to pay the price of your predecessor's laxness.
In Oracle, you can use all_tab_cols to query the columns names of your tables. The following query will return the common column names between TABLE1 and TABLE2. Then you just need to prefix those common columns instead of all 100 column references.
select column_name from all_tab_cols
where table_name='TABLE1' and owner ='OWNER1'
and column_name in (
select column_name from all_tab_cols
where table_name='TABLE2' and owner ='OWNER2')
For posterity's sake:
I had this issue when I selected columns TABLE1.DES and TABLE2.DES in a query without aliasing the result. When I ran it alone my SQL editor turned these into DES and DES_1, no complaint.
However when I turned the same query into a subquery
SELECT a.col1, a.col2, a.col3, b.*
from TABLE3 a
INNER JOIN (
--that query as a subquery
) b
on a.PK=b.FK`
it threw the same ORA-00918 error message you described. Changing the SELECT in my subquery to
SELECT TABLE1.DES AS T1_DES, TABLE2.DES AS T2_DES ...
fixed the issue.
you can check common columns by using :
select COLUMN_NAME from ALL_TAB_COLS where TABLE_NAME = 'tablenamefirst'
intersect
select COLUMN_NAME from ALL_TAB_COLS where TABLE_NAME = 'tablenamesecond';

Resources