简体   繁体   English

Oracle:如何从动态生成的查询中获取最大列长?

[英]Oracle : How to get max column length from dynamically generated query?

Suppose I have a query like 假设我有一个类似的查询

select a.column1, b.column2, substr(b.column3,instr(b.column3,',')) as Column3
from tableA a, tableB b
where a.column1 = b.column1

How do I get the max character length for each column of this query ? 如何获取此查询每一列的最大字符长度?

If tableA and tableB are defined as 如果将tableA和tableB定义为

tableA ( column1 VARCAHR2(100))     
tableB ( column1 VARCAHR2(100), column2 INTEGER, column3 VARCAHR2(5000))

I would like to get those data : 我想获取这些数据:

COLUMN1 : 100
COLUMN2 : 10
COLUMN3 : 5000

Keep in mind this is a dynamic query, there might be any number of columns from any number of tables within limitation. 请记住,这是一个动态查询,在限制范围内,可能有来自任何数量的表的任何数量的列。

You can create a table for a moment from your query (CTAS) and after that use user_tab_columns to get column's data_length for your new table. 您可以从查询(CTAS)中创建一个表,然后使用user_tab_columns获取新表的列的data_length

create table temp as 
select a.column1, b.column2, substr(b.column3,instr(b.column3,',')) as Column3
from tableA a, tableB b
where a.column1 = b.column1
    --No need for store the data, unless you want the maximum size of current data.
    and 1 = 0;

select column_name, data_length from user_tab_columns where table_name = 'TEMP';

drop table temp;

It's quite easy and you do not need to care what you have exactly in your query. 这非常容易,您无需关心查询中的确切内容。 Oracle will figure out this for you. Oracle将为您解决这个问题。

BTW. BTW。 The max size of VARCHAR2 is 4000. You cannot have 5000 unless you are using 12c with extended data types. VARCHAR2的最大大小为4000。除非使用扩展数据类型的12c,否则不能有5000。

You might have to access the USER_TAB_COLUMNS metadata table to get these column lengths 您可能必须访问USER_TAB_COLUMNS元数据表才能获取这些列的长度

Below is an example query 以下是查询示例

SELECT COLUMN_NAME
      ,CHAR_LENGTH
  FROM USER_TAB_COLUMNS 
 WHERE TABLE_NAME IN ('tableA', 'tableB')
   AND COLUMN_NAME IN ('column1', 'column2', 'column3')
;

If you need the above in your generated query, then it would need to change to something like below 如果在生成的查询中需要以上内容,则需要将其更改为以下内容

select a.column1
     , b.column2
     , substr(b.column3,instr(b.column3,',')) as Column3
     , (SELECT CHAR_LENGTH FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'tableA' AND COLUMN_NAME = 'column1') AS column1_size
     , (SELECT CHAR_LENGTH FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'tableB' AND COLUMN_NAME = 'column2') AS column2_size
     , (SELECT CHAR_LENGTH FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'tableB' AND COLUMN_NAME = 'column3') AS column3_size
  from tableA a
     , tableB b
 where a.column1 = b.column1

The above is one way of doing this just as an example. 以上只是作为示例的一种方法。 Depending on what you are trying to do and how you are generating the query, I think you would want to get the column sizes in a separate query like the first one, since you already know the column and table names when generating the query. 根据您要执行的操作以及生成查询的方式,我认为您希望像第一个查询一样在单独的查询中获取列大小,因为在生成查询时您已经知道列名和表名。

Hope this helps.. 希望这可以帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM