So I have this small problem with Oracle 12c.
Whenever I do a query like
SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE table_name = 'EMP';
I get the column names in the right order
empno
ename
...
but when I run it again the column names get reversed.
deptno
comm
...
ename
empno
Anyone knows why this happens? Is this a new "feature" implemented in 12c or it's just me that get this the wrong way? And most important is there a way to fix this?
Thanks in advance and sorry if this is a dumb question.
Use
order by column_id;
and you will allways get the right order of columns
Related
Problem: I want to get an average salary of all employees. The query itself is rather simple:
Question: Is it possible to move COUNT(salary) to selected column's title (after AS statement), so the name of the column will be "Average salary of 20 employees"? I've been trying several approaches, but none of them worked. I would really appreciate any help. Thank you in advance.
Screenshot you posted looks like Apex SQL Workshop. I don't know how to do it there, but - if you used SQL*Plus, then you could do something like this (not exactly as you wanted it, though):
SQL> set ver off
SQL> column cnt_emps new_value l_cnt
SQL> select count(*) cnt_emps from emp;
CNT_EMPS
----------
14
SQL> select avg(sal) as "Average salary of &l_cnt employees" from emp;
Average salary of 14 employees
--------------------------------------
2073.21429
SQL>
However, if you're already using Apex, switch to GUI and create a page which will let you format the result any way you want (using colors, large font, bold letters, whatever). SQL*Plus and its descendants are kind of restrictive when such things should be done.
I usually have to check datatype a particular tablecolumn via a sql code, for which I use
desc tablename
Some tables I look at have a lot of columns, and I think it would be nice if there's a way to get the description for only a particular column (or columns).
Does anyone know of a way to get that info for only a particular (set of) column(s)?
For example something like the beneath would be nice, and possibly there already is?
desc tablename.column_name
Adding on the answer provided by Radim , You not only need the datatype but also the precision for when it comes to datatypes like varchar2(20).
Select TABLE_NAME,COLUMN_NAME,DATA_PRECISION From ALL_TAB_COLUMNS
Where TABLE_NAME = UPPER('TABLE_NAME') and COLUMN_NAME = UPPER('COLUMN_NAME')
Use ALL_TAB_COLUMNS system catalog views
Select COLUMN_NAME, DATA_TYPE From ALL_TAB_COLUMNS
Where TABLE_NAME = UPPER('TAB NAME') and COLUMN_NAME = UPPER('COL NAME')
If I run the following query:
select count(*) from all_tab_columns
where column_name = 'foo'
and table_name = 'VIEW0';
I get 0 for a result. I expect 1.
But if I run the following query I get many (expected) rows returned:
select foo from VIEW0;
Why? I'm assuming I'm making some dumb syntax mistake or my understanding is way off.
Probably the reason is that you have case sensitive setting.
Try to add UPPER function as below.
select count(*) from all_tab_columns
where column_name = upper('foo')
and table_name = 'VIEW0';
ALL_TAB_COLUMNS describes the columns of the tables, views, and clusters accessible to the current user. Check, if user under whom you running this query have access to the desired table.
It appears, at least in 11g, that you cannot access the data dictionary tables from PL/SQL. Running any select on all_tab_columns inside PL/SQL always returns no results. Attempting to access dba_tab_columns will not compile, because the compiler believes the table (or view) does not exist.
I'd love to see how to access the data dictionary from PL/SQL.
I created like 20 tables in sql 11g and lost the record of them. Is there any way I can list the table names I created.
SELECT table_name FROM user_tables is not the solution.
First, log on as sys before you can view the tables in the entire database.
Second, run this query using the sys
SELECT owner, table_name FROM dba_tables WHERE owner='HR';
This should display all tables owned by the HR schema. Remember to use uppercase for the owner (HR) else you will receive error.
Hope this helps.
The Best approach to solve this issue is considering the attribute of ALL_ALL_TABLES named
LAST_ANALYZED
It gives Date on which the table was most recently analyzed.
So you could easily query the database with the help of DATE Functions.
The Oracle Database Reference helps you.
SELECT TABLE_NAME FROM USER_ALL_TABLES
Well, if user_all_tables is not your desired result, you will have to rely on dba_tables via:
SELECT owner, table_name
FROM dba_tables
But for this you need more privs than for user_tables obviously
Can someone explain things a little better to me? How do I show the structure of a table?
I run the select * from table; and of course it displays all that's in the table. But, I am being asked to show the structure of the table. What does that mean, and what is the command?
Here's my table below.
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
Try this out: describe table_name
To list columns and data types, I typically use
SELECT COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS WHERE TABLE_NAME='your_table_name';
It's been a while since I've worked with Oracle though. ALL_TAB_COLUMNS might actually be ALL_TAB_COLS.
If you need to display the full CREATE TABLE statement, see How to get Oracle create table statement in SQL*Plus
You can use sqlplus command describe <SCHEMA_OWNER.TABLE>
https://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12019.htm