Get Columns Names of Table - sql

How I can Get for a specific Table its columns Names ?
I tried this :
SELECT column_name
FROM USER_TAB_COLUMNS
WHERE table_name = 'x' ;
But it doesn't work.

Try this :
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TABLENAME'
Hope this helps.
EDIT :
The Oracle equivalent for information_schema.COLUMNS is USER_TAB_COLS for tables owned by the current user, ALL_TAB_COLS or DBA_TAB_COLS for tables owned by all users.
Tablespace is not equivalent to a schema, neither do you have to provide the tablespace name.
Providing the schema/username would be of use if you want to query ALL_TAB_COLS or DBA_TAB_COLS for columns OF tables owned by a specific user. in your case, I'd imagine the query would look something like:
String sqlStr= "
SELECT column_name
FROM all_tab_cols
WHERE table_name = 'users'
AND owner = ' || +_db+ || '
AND column_name NOT IN ( 'password', 'version', 'id' )
"
Note that with this approach, you risk SQL injection.
Source : Oracle query to fetch column names

String comparisons in Oracle are case sensitive by default, and table and column names are upper case by default, so you need to make sure that the capitalization of the table name you are searching for matches the the way it's stored in the database, so unless your table was named with mixed case or all lower case try making sure your string is all upper case.
SELECT column_name from USER_TAB_COLUMNS
WHERE table_name = 'X'; -- not 'x'
Additionally, if you don't own the table, then you need to use either ALL_TAB_COLUMNS or DBA_TAB_COLUMNS instead of USER_TAB_COLUMNS since USER_TAB_COLUMNS only lists details for tables owned by your current schema.

You can just describe the table:
desc x;

Try to queryUSER_TAB_COLUMNS view
SELECT column_name
FROM USER_TAB_COLUMNS
WHERE table_name = 'TABLE_NAME'

Related

Need to add to srting in postgresql select query result

i have a bundle of tables in my database. I need to truncate all the tables but not some.
so by using table_schema i can get list of table names!
select ''as "truncate", table_name
from information_schema.tables
where table_schema = 'public'
Expected output.
truncate table table_name restart identity.
some one tell me is there any better way to do that.
Use format() with placeholders for the schema and table name to make sure the names are quoted properly:
To properly deal with the foreign keys used by the tables, it would also be better to add the cascade option to the truncate command:
select format('truncate table %I.%I restart identity cascade;', table_schema, table_name) as stmt
from information_schema.tables
where table_schema = 'public';
Another option would be to truncate all tables in a single statement:
select concat('truncate table ', string_agg(format('%I.%I', table_schema, table_name), ', '), ' restart identity;') as stmt
from information_schema.tables
where table_schema = 'public'
You can concat strings in Postgres using ||
select 'truncate table' || table_name::text || ' restart identity'
from information_schema.tables
where table_schema = 'public
Take a look at 9.4. String Functions and Operators

How to use the system catalogue view

I have been tasked to find the names of relational tables together with the names of its attributes, data types of attributes, NULL/NOT NULL constraints, and column ids of all tables owned by my account. How do i go about doing that?
I have tried,
SELECT * FROM information_schema.SCHEMATA S;
but got
ERROR at line 1:
ORA-00942: table or view does not exist
Also tried
SELECT table_name,
column_name,
data_type,
data_length "LENGTH",
data_precision "PRECISION",
FROM user_tab_columns WHERE table_name = 'PART'
ORDER BY user_tab_columns.table_name, column_name;
FROM user_tab_columns WHERE table_name = 'PART'
*
ERROR at line 6:
ORA-00936: missing expression
Can anyone give me an idea what should i do?
Please try
SELECT *
FROM information_schema.columns
Your second formulation is basically correct. Your error is because of a comma before the FROM:
SELECT table_name, column_name, data_type, data_length as "LENGTH",
data_precision as "PRECISION"
------------------------------------^
FROM user_tab_columns
WHERE table_name = 'PART'
ORDER BY table_name, column_name;
The INFORMATION_SCHEMA views are ANSI standard views for metadata available in many databases. However, Oracle does not (yet?) support them. So you need to use Oracle's metadata views and tables for this purpose.

How to extract table definitions using SQL or Toad

Can somebody tell me how to extract my table definitions using SQL? I want to extract the datatypes of all my tables and other information from my Oracle schema. I have about 100 tables.
I need the complete documentation of my Oracle Schema. My schema name IS "cco".
Can I do this by SQL?
I am using Toad for Data analyst 3.3. Please let me know if this tool helps.
You can try this -
select * from all_tab_cols
where owner = 'CCO';
To get the DDL for all tables of the current user, you can use this:
select dbms_metadata.get_ddl('TABLE', table_name)
from user_tables;
You will need to adjust your SQL client to be able to properly display the content of a CLOB column.
More details (e.g. about how to get the DDL for other objects) can be found in the manual: http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_metada.htm
you can use the table:USER_TAB_COLUMNS
Find below query example
select
table_name,
column_name,
data_type,
data_length,
data_precision,
nullable
from USER_TAB_COLUMNS
where table_name = '<table_name>';
This is only an example you can also do a select * to get more information.
you can also use the table: all_tab_columns
For a better display you can use:
select table_name,column_name, data_type||
case
when data_precision is not null and nvl(data_scale,0)>0 then '('||data_precision||','||data_scale||')'
when data_precision is not null and nvl(data_scale,0)=0 then '('||data_precision||')'
when data_precision is null and data_scale is not null then '(*,'||data_scale||')'
when char_length>0 then '('||char_length|| case char_used
when 'B' then ' Byte'
when 'C' then ' Char'
else null
end||')'
end||decode(nullable, 'N', ' NOT NULL') as data_type
from user_tab_columns
where table_name = '<TABLE_NAME>';

Search command to search for Column/field in SQLPLUS for Oracle 10.2

I want to search for column/field called ACC
I have 28 users. I got that information by this command; to list all schemas:
SELECT username FROM all_users ORDER BY username;
I am connected to user # 13 in the list called 'ABCD', by this command: SHOW USER;
Here is a detailed structure.
user1 > Has many tables > each table has many column/field.
user2 > Has many tables > each table has many column/field.
user3 > Has many tables > each table has many column/field.
and so on.
I need to search for field or column which is called ACC
You need to use ALL_TAB_COLUMNS or DBA_TAB_COLUMNS, depending on your access rights.
The following will get you every table, which has the column acc in it and the user who "owns" that table.
select owner, table_name
from dba_tab_columns
where column_name = 'ACC'
ALL_TAB_COLUMNS will display all the tables (and their columns) that are accessible to the user you are logged in as. DBA_TAB_COLUMNS displays them for everything in the database.
If I create a dummy table so I know I have some matching data:
create table t42 (field_acct_1 number, field_acct_2 number);
insert into t42 values (123, 456);
insert into t42 values (234, 567);
insert into t42 values (678, 123);
insert into t42 values (123, 123);
I can a cursor and bulk collect (to allow for duplicate values) to look for matches, based on the recent answer mentioned in comments:
set serveroutput on
declare
type t_values is table of number;
l_values t_values;
cursor c1(cp_value number) is
select owner, table_name, column_name,
'select "' || column_name
|| '" from "' || owner ||'"."'|| table_name
|| '" where "' || column_name || '" = ' || cp_value
as query
from all_tab_columns
where column_name like '%ACC%'
and owner != 'SYS'
and data_type = 'NUMBER'
order by owner, table_name, column_name;
begin
for r1 in c1(123) loop
execute immediate r1.query bulk collect into l_values;
for i in 1..l_values.count loop
dbms_output.put_line(r1.owner
||'.'|| r1.table_name
||'.'|| r1.column_name
||':'|| l_values(i));
end loop;
end loop;
end;
/
I've restricted it to look for NUMBER columns to save time, and excluded SYS tables, but you can problem add filters to reduce the work it has to do even more. Other than that it's pretty much the same idea; see the embedded comments in the earlier answer to see what's happening if it isn't clear.
And that gives me (as owner.table.column:value):
SCOTT.T42.FIELD_ACCT_1:123
SCOTT.T42.FIELD_ACCT_1:123
SCOTT.T42.FIELD_ACCT_2:123
SCOTT.T42.FIELD_ACCT_2:123
I was able to figure it out myself.
SELECT
COLUMN_NAME
FROM USER_TAB_COLUMNS
WHERE COLUMN_NAME like '%ACC%';
I can also find out the table name by selecting table_name
SELECT
COLUMN_NAME,
TABLE_NAME
FROM USER_TAB_COLUMNS
WHERE COLUMN_NAME like '%ACC%';
Following Query is selected from different table, it has more result and shows OWNER also.
SELECT
distinct owner,
COLUMN_NAME,
TABLE_NAME
FROM all_tab_columns
WHERE COLUMN_NAME like '%ACC%'
To search for Table
SELECT
COLUMN_NAME,
TABLE_NAME
FROM USER_TAB_COLUMNS
where TABLE_NAME like '%X%'

Oracle - how to find columns in a table + stored procedures dependent on them?

Scenario:
I need to list all the columns in a table1 and all stored procedures that depends on those columns of this table1. I need to populate column name and stored procedures to a new table.
I created new_table(col1, sProc) and tried to populate the column name and respective stored procedure on this new_table. Code I wrote is given below:
Declare
Begin
for i in (select column_name from user_tab_columns where lower(table_name) like 'table1') loop
insert into new_table
select i.column_name,
name
from user_source
where lower(text) like '%' || i.column_name || '%';
commit;
end loop;
end;
Result:
The scripts run successfully but the no data is populated on this new_table.
Stress:
I tried to solve it for 1 whole day and could not figure it out. Any help on this would be greately appreciated. Thank you once again.
One obvious problem is that you're converting the procedure text to lowercase, but not the column name you're looking for.
This code has other problems, however. What happens if the column name happens to match some part of the text that isn't a column reference?
The best you will be able to do is to list the package name (as that is the value in the USER_SOURCE.NAME field) along with the column. As rexem indicates in his comment, you dont need to resort to a for loop:
INSERT INTO new_table (col1, sproc)
SELECT i.column_name, u.name
FROM user_tab_columns i,
user_source u
WHERE lower(i.table_name) like 'table1'
AND lower(u.text) like '%' || lower(i.column_name) || '%';
You can reduce the false positives by including USER_DEPENDENCIES in the query. You may want to restrict the searched types (or alternatively include TYPE in NEW_TABLE).
insert into new_table (col1, sproc)
select distinct tc.column_name, sp.name
from user_tab_columns tc
, user_source sp
, user_dependencies d
where d.referenced_name = 'TABLE1'
and d.referenced_type = 'TABLE'
and d.type IN ('PACKAGE', 'PACKAGE BODY', 'FUNCTION'
, 'PROCEDURE', 'TYPE', 'TRIGGER')
and tc.table_name = 'TABLE1'
and sp.name = d.name
and instr(lower(sp.text), lower(tc.column_name)) > 0
/