I am migrating a database and am trying to retrieve table structure information into a single line item for machine processing. For technical reasons, existing migration tools can not be used and it must be processed in this manner.
I have run the following query with success on multiple tables:
SELECT LISTAGG(column_name || ',' || data_type || ',' || data_length, ',')
WITHIN GROUP (ORDER BY column_id)
FROM all_tab_cols
WHERE table_name = 'MyTableName'
For many of the tables it works fine and returns exactly what I would expect:
ColumnName1, VARCHAR2, 20, ColumnName2, NUMBER, 22, ColumnName3, CHAR, 3, ...
However, sometimes it is not quite right. For example, I know from the DB design document that ColumnName2 is supposed to be a number of length 2, not 22. Why does that return incorrectly?
And even more puzzling, sometimes it does not work at all and returns nothing. I was thinking it was the CHAR data type that was causing problems, but some of my tables that have CHAR worked alright. It does seem to pretty consistently give me problems if they are INTEGER, SHORTINT, or DATE types though. What is the best way to fix this problem?
I also know the table exists, because when I run a simple
SELECT * FROM MyTableName
it returns all of the records in the table.
UPDATE
I tried replacing the data_length with data_precision and for numbers it returned the correct answer, but now I have nothing for VARCHAR2. How do I need to restructure my query to get me data_precision if it is a number and instead give me data_length if it is anything else?
Also, I still have several tables that will not let me view their structures. I am still not very familiar with schemas, but I understand sometimes a table may exist under a different one. But why would the data be returned with a SELECT *, but the structure information here would not when I am looking at all_tab_col?