1

I need to find the column info for a number of Oracle views using JavaScript. What I've tried is:

var conObj = new ActiveXObject('ADODB.Connection');     
conObj.Open(conString);     
sql = "DESC MyView";
rs1 = new ActiveXObject("ADODB.Recordset");

var commd = new ActiveXObject('ADODB.Command');

commd.ActiveConnection = conObj; //

commd.CommandText = sql;

commd.CommandType = 1;

rs1 = commd.Execute();

What I get is an error about the sql statement. I know the sql works in SQL Developer.

I can't use the alternative SELECT query as that returns an empty rowset - the view isn't populated when I need to run the query. Only the DESC returns the values.

Any ideas?

2
  • Where is this code supposed to run? Where is the database server? Commented Jun 24, 2014 at 17:53
  • DESC is a SQL*Plus command. SO, you cannot use it. Commented Jun 24, 2014 at 17:53

2 Answers 2

8

DESC is a SQL*Plus command. SO, you cannot use it via JDBC/ODBC. An alternative can be like this below.

select RPAD(COLUMN_NAME,30)||' '||DATA_TYPE||'('||DATA_LENGTH||')' as descr
FROM all_tab_cols
  WHERE TABLE_NAME = UPPER('YOUR_TABLE') and owner=UPPER('SCHEMA_NAME');

Oracle's Reference

all_tab_cols is a data dictionary table(view) which contains the table/view metadata.

Example:

SQL> create view MyView as select * from dual where 1=2;

View created.

SQL> select RPAD(COLUMN_NAME,30)||' '||DATA_TYPE||'('||DATA_LENGTH||')' as descr
  2  FROM all_tab_cols
  3   WHERE TABLE_NAME ='MYVIEW'  and owner='MYSCHEMA';


DESCR
------------------------------------------------
DUMMY                          VARCHAR2(1)

Data and DESC

SQL> select * from myview;

no rows selected

SQL> desc myview;

 Name       Null?    Type
 -----------------------------
 DUMMY               VARCHAR2(1)
Sign up to request clarification or add additional context in comments.

13 Comments

I just tried plugging that in and it didn't like the FROM location. I have tried : sql = "select * from user_tab_cols where table_name = 'mytable'" but that comes back empty.
The alias ,I chose was actually a reserved keyword,and hence it is. Try now please. Also, the table name has to be all CAPS
You could double quote the alias, "desc". Also, data_length is of type NUMBER (Oracle implicitly cast this as a varchar).
That gets past the problem with the FROM but it returns an empty rowset. I think the problem there is that there aren't any rows in the view. It isn't populated until later. The DESC command does work - I assume it is using something deeper in Oracle. When I run the DESC command I get a text string describing the table but I don't know how I would extract that.
@Patrick double quotes would work, but I personally dont use just because, while fetching I have to use the same case. (if I dont choose the column index). And type casting wont make an impact here I guess. But still user can include individual columns as well.
|
0

I agree with Maheswaran's query. Additionally, I tried the below query to get all the information. This lets me know about the different constraints and attributes.

select * FROM all_tab_cols WHERE TABLE_NAME = UPPER('TABLE_NAME') and owner=UPPER('SCHEMA_NAME');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.