473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How to get Oracle table field definition

Hi,

from SQL*Plus, i use:

DESCRIBE MyTable

and I get this result (example):

Nom NULL ? Type
----------------------------------------- -------- ------------------------
----
FIELD1 NOT NULL NUMBER(38)
FIELD2 VARCHAR2(30)

From PHP, the same command output:

DESCRIBE MyTable

Execute failed: ORA-00900: invalid SQL statement
Any idea?

TB
--
Click here to answer / cliquez ci dessous pour me repondre
http://cerbermail.com/?7O7SOrggJg
Jul 17 '05 #1
3 35798
The simple answer is that "DESCRIBE" is not standard SQL. RDBMS systems often provide an application to do direct database interaction where you can type SQL directly, but also generally expose a number of other convenience commands. We use PostgreSQL here, and so for example I can simply type "\dt" to get a list of all my tables. I can type "\d <table_name>" to get a description of a particular table. Clearly, "\dt" is not standard SQL to describe a table.

What you execute through PHP does not interact with the SQL*Plus application - it talks SQL directly to the database, and so all these extra convenience commands are not directly available (they are no-doubt implemented behind the scenes in terms of SQL, but it may be fairly complex and there may not be any easy way to see what the implementation is - I don't know).

I wanted to get the same sort of information on my system, and so I had a co-worker who is more adept at SQL write the following query for me:

/* Select statement that will give you the metadata for a particular
table, assuming you have/know the table name at the time of execution

To use replace the 'well' below on the 5th line of the statement
with the table name.

This probably won't work on temporary tables (part of a stored procedure or built on the fly). Might work on a view, I haven't tried.

typnam meanings:
bpchar = character
int4 = integer
int8 = longint

attlen meanings:
-1 = variable length
number of bytes used in storage
*/
SELECT a.attnum, a.attname, t.typname, a.attlen
FROM pg_class as c, pg_attribute a, pg_type t
WHERE a.attnum > 0
AND a.attrelid = c.oid
AND c.relname = 'MyTable'
AND a.atttypid = t.oid
ORDER BY a.attnum;
When executed on our system, substituing 'MyTable' with a valid table name, it produces the following output:
=> \i columns_metadata.txt
attnum | attname | typname | attlen
--------+-------------+---------+--------
1 | well_api_id | bpchar | -1
2 | operatorid | int4 | 4
3 | leaseid | int4 | 4
4 | well_type | bpchar | -1
5 | depth_ft | int8 | 8

'attname' here is a column of column names, typname is the data type for that column, attlen is the size of that column.

So, the good news is that all of the same data is no doubt stored on your system in various meta-data tables. The bad news is that it is probably all under differently named tables with differently named columns, so I don't expect you will be able to run the query above. You will have to dig in and find out what your meta-data tables are named and what their column names are, and figure out how to put together a similar SQL query that will select what you need.

-ej

"Thierry B." <no****@nospam.com> wrote in message news:c2**********@news-reader5.wanadoo.fr...
Hi,

from SQL*Plus, i use:

DESCRIBE MyTable

and I get this result (example):

Nom NULL ? Type
----------------------------------------- -------- ------------------------
----
FIELD1 NOT NULL NUMBER(38)
FIELD2 VARCHAR2(30)

From PHP, the same command output:

DESCRIBE MyTable

Execute failed: ORA-00900: invalid SQL statement


Any idea?

TB
--
Click here to answer / cliquez ci dessous pour me repondre
http://cerbermail.com/?7O7SOrggJg

Jul 17 '05 #2

"Thierry B." <no****@nospam.com> wrote in message
news:c2**********@news-reader5.wanadoo.fr...
Hi,

from SQL*Plus, i use:

DESCRIBE MyTable

and I get this result (example):

Nom NULL ? Type
----------------------------------------- -------- ---------------------- -- ----
FIELD1 NOT NULL NUMBER(38)
FIELD2 VARCHAR2(30)

From PHP, the same command output:

DESCRIBE MyTable

Execute failed: ORA-00900: invalid SQL statement
Any idea?

TB
--
Click here to answer / cliquez ci dessous pour me repondre
http://cerbermail.com/?7O7SOrggJg


Depends on what version you are using so in Oracle9i you can use:

select dbms_metadata.get_ddl( 'TABLE', 'MyTable', 'OraUserName' ) from dual;

In other versions of Oracle, take a look at the user_tab_columns view and do
something like this:

select column_name,data_type from user_tab_columns where table_name =
MyTable;

Note also that you need the 'resource' role granted to you (in addition to
connect role) by the dba in order to get metadata from the data dictionary.

Brandon
Jul 17 '05 #3
On Wed, 17 Mar 2004 03:12:50 GMT, "Brandon" <br**********@mchsi.com> wrote:
"Thierry B." <no****@nospam.com> wrote in message
news:c2**********@news-reader5.wanadoo.fr...

DESCRIBE MyTable

and I get this result (example):

Nom NULL ? Type
----------------------------------------- -------- ------------------------
----
FIELD1 NOT NULL NUMBER(38)
FIELD2 VARCHAR2(30)

From PHP, the same command output:

DESCRIBE MyTable

Execute failed: ORA-00900: invalid SQL statement


Depends on what version you are using so in Oracle9i you can use:

select dbms_metadata.get_ddl( 'TABLE', 'MyTable', 'OraUserName' ) from dual;

In other versions of Oracle, take a look at the user_tab_columns view and do
something like this:

select column_name,data_type from user_tab_columns where table_name =
MyTable;


With quotes around 'MYTABLE' and the name in uppercase, most likely - unless
it was created as "MyTable".
Note also that you need the 'resource' role granted to you (in addition to
connect role) by the dba in order to get metadata from the data dictionary.


No you don't. Anyone can access USER_TAB_COLUMNS as SELECT is granted to
PUBLIC and there's a public synonym (unless your DBA is paranoid and has
removed them).

SQL> create user unprivileged identified by unprivileged;

User created.

SQL> grant create session to unprivileged;

Grant succeeded.

SQL> connect unprivileged/unprivileged;
Connected.
SQL> select * from user_tab_columns;

no rows selected
In fact CONNECT and RESOURCE don't have any DML privileges, they only have
system privileges such as CREATE TABLE etc.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marcus Leon | last post by:
Access freezes when we attempt to link to and then open an Oracle table that has a Timestamp column. Does anyone know why? This issue does not occur if you attempt to link to and open a table...
1
by: Diffident | last post by:
Howdy, How can I set the CacheDependency to an Oracle table? I have come across SqlDependency class for using the CacheDependency to an SQL server table but not Oracle. Any pointers? ...
2
by: rcc.gamer | last post by:
Hello All, I am trying to connect a GridView to an Oracle table using the standard SqlDataSource. The process is fairly simple and there are detailed instructions from Microsoft ......
3
by: Roger | last post by:
My application connects to a DB2 V8.2 EE database running on AIX. I need to now lookup a table in SQL SERVER via the DB2 connection. I also need to run an sql that joins a DB2 table with an...
7
by: getchandan | last post by:
How can I open an Oracle table in a javascript event like "onblur" of a text box.
0
by: NasirMunir | last post by:
I am trying to compare records from oracle table with a table in access. With oraDynaset, I have the option of .findNext function. I can use that to look for matching records from access to oracle....
4
by: avellas | last post by:
Hi, I was wondering if anyone would know how to insert the copyright character "©" into a field of an Oracle table? Many thanks in advance... Sam
3
by: rmurgia | last post by:
I created an ODBC connection with the Microsoft ODBC for Oracle driver and successfully connected to Oracle from Microsoft Access by creating a pass through query. Can I use this connection from...
4
by: goodamr | last post by:
Hi, I have many Excel files (more than 200 files). I'm using TOAD to import the Excel files into the Oracle table, but this process is boring and time-consuming especially for this huge number of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.