1

谢谢你的时间!

我使用OCI8连接Oracle并执行一些sql语句。

但是当我执行以下操作时:

conn = OCI8.new('ci/a@localhost/orcl')
cursor = conn.parse('desc table_name')
cursor.exec

它引发了错误:

OCIError: ORA-00900: invalid SQL statement

我问了一些 DBA,他们告诉我这desc是 Data Definition Language (DDL) ,它不是普通的 SQL,可能这就是导致问题的原因。

我使用 Ruby 作为我的脚本语言。我该如何解决这个问题?

4

1 回答 1

1

desc不是DDL。这是一个 sqlplus 命令。

使用OCI8#describe_table或字典视图如下:

conn.describe_table('TABLE_NAME').columns.each do |col|
   puts format('%-30s %s', col.name, col.data_type_string)
end

或者

conn.exec("select column_name, data_type
             from all_tab_columns
            where owner = 'OWNER_NAME'
              and table_name = 'TABLE_NAME'
            order by column_id") do |row|
  puts format('%-30s %s', row[0], row[1])
end

前者适用于表格、视图和同义词。后者仅适用于表格。

于 2013-11-29T11:02:21.913 回答