使用 JavaScript 在 Oracle 中执行“DESC TABLE”命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24393191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute a "DESC TABLE" command in Oracle using JavaScript?
提问by Frank Perry
I need to find the column info for a number of Oracle views using JavaScript. What I've tried is:
我需要使用 JavaScript 查找许多 Oracle 视图的列信息。我试过的是:
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.
我得到的是关于 sql 语句的错误。我知道 sql 在 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.
我不能使用替代的 SELECT 查询,因为它返回一个空的行集 - 当我需要运行查询时没有填充视图。只有 DESC 返回值。
Any ideas?
有任何想法吗?
回答by Maheswaran Ravisankar
DESC
is a SQL*Plus
command. SO, you cannot use it via JDBC/ODBC.
An alternative can be like this below.
DESC
是一个SQL*Plus
命令。所以,你不能通过 JDBC/ODBC 使用它。另一种选择如下。
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');
all_tab_cols
is a data dictionary table(view) which contains the table/view metadata.
all_tab_cols
是包含表/视图元数据的数据字典表(视图)。
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
数据和 DESC
SQL> select * from myview;
no rows selected
SQL> desc myview;
Name Null? Type
-----------------------------
DUMMY VARCHAR2(1)
回答by Varun
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.
我同意 Maheswaran 的询问。此外,我尝试了以下查询来获取所有信息。这让我知道不同的约束和属性。
select * FROM all_tab_cols WHERE TABLE_NAME = UPPER('TABLE_NAME') and owner=UPPER('SCHEMA_NAME');
select * FROM all_tab_cols WHERE TABLE_NAME = UPPER('TABLE_NAME') and owner=UPPER('SCHEMA_NAME');