Question

Thanks for your time!

I use OCI8 to connect Oracle and execute some sql statement.

But when I do as following:

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

It raised the error:

OCIError: ORA-00900: invalid SQL statement

I've asked some DBA and they told me that desc is Data Definition Language (DDL) , which is not a normal SQL, and probably that's what caused the issue.

I use Ruby as my scripting language. How could I solve this problem?

Was it helpful?

Solution

desc is not DDL. It is a sqlplus command.

Use OCI8#describe_table or dictionary views as follows:

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

or

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

The former works for tables, views and synonyms. The latter works only for tables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top