简体   繁体   English

如何在执行查询之前获取SQL中的列数? 即ResultSetMetaData以外的其他方式

[英]How to get column count in SQL before executing the query? i.e. way other than ResultSetMetaData

I have a sql query. 我有一个SQL查询。 Because of too many BO's are involved, it's forming very big query containing more than 1000 columns. 由于涉及的BO过多,因此它形成了包含1000多个列的非常大的查询。 So I need some way with which I can find column count before hand and then apply some logic to handle 1000 column error. 因此,我需要某种方式可以事先找到列数,然后应用一些逻辑来处理1000列错误。

May be parsing a query to get column count would help. 可能解析查询以获取列数会有所帮助。 But not getting how to implement it. 但是没有得到实现的方法。

Note: tried below code already, it's giving me result, but i dont want to execute query, that's costly. 注意:已经在下面的代码中尝试过,它给了我结果,但是我不想执行查询,这很昂贵。

PreparedStatement pstmt;
    try {
        pstmt = getConnection().prepareStatement(sql);
        ResultSetMetaData meta = pstmt.getMetaData();
        System.out.println(meta.getColumnCount());

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

If it is a plain old select query like : 如果它是一个普通的旧选择查询,例如:

select col1,col2 as xyz, col3,col4... from ... where ...

you can do something like this: 您可以执行以下操作:

String select="select";//"SELECT" if yours is in caps
String from="from";//"FROM" if yours is in caps
String cols = sql.substring(sql.indexOf(select)+select.length(), sql.indexOf(from)); 
int colCount = cols.split(",").length;

If your query contains functions like fn(col,'blah blah') you need to write some extra code to ID such functions and process things appropriately. 如果您的查询包含fn(col,'blah blah')类的函数,则需要编写一些额外的代码来标识此类函数并适当地进行处理。

I guess your SQL query using lot of wildcards after SELECT statement right? 我猜您的SQL查询在SELECT语句后使用了很多通配符,对吗? If no wildcard used, you can parse SQL query part between SELECT and FROM to get column numbers.I feel some disturbance in the force if i hear you have such big results of unknown attributes. 如果不使用通配符,则可以在SELECT和FROM之间解析SQL查询部分以获取列号。如果我听到您对未知属性有如此大的结果,我会感到有些不安。 If we can say that SQL table is data source, we can say that query result is data source too. 如果我们可以说SQL表是数据源,那么我们也可以说查询结果也是数据源。 In case of SQL table we have defined very specifically what columns(attributes) it contain. 对于SQL表,我们已经非常明确地定义了它包含哪些列(属性)。 If u create SQL query without specification of columns you have data source of unknown entity. 如果您创建不带列说明的SQL查询,则您具有未知实体的数据源。 Are you trying to create some BI system? 您是否要创建一些BI系统? If yes you should consider to get metadata of table and create specific query based on this informations. 如果是,则应考虑获取表的元数据并根据此信息创建特定的查询。

暂无
暂无

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

相关问题 如何有效地获取“ AS”列名称,例如ResultSetMetaData? - How to effectively get “AS” column name like ResultSetMetaData? Count()/ sum()的ResultSetMetadata列类型 - ResultSetMetadata column types for count()/sum() 使用ResultSetMetaData构架SQL查询 - Using ResultSetMetaData to frame SQL query 如何从JAVA中的ResultSet或ResultSetMetaData对象获取数据库表的主键的列名? - How can I get the column name of the primary key of a Database Table from the ResultSet or ResultSetMetaData object in JAVA? 我可以在联接选择查询表单ResultSetMetaData中获取表名吗 - can i get table name in join select query form ResultSetMetaData 如何从Java中的ResultSetMetaData获取不同的数据类型? - How can I get different datatypes from ResultSetMetaData in Java? 如何获得具有值的XML文档的最底层子元素(即没有其他子元素)? - How to get bottom-most child elements of XML document that have a value (i.e. no other child elements)? HSQLDB和DBUnit,在SQL查询上使用Allias时无法从ResultSetMetaData中找到列 - HSQLDB & DBUnit, Cannot find column from ResultSetMetaData when using allias on SQL query DB2和ResultSetMetaData - 无法获取列名 - DB2 and ResultSetMetaData - Unable to get column name 如何以原子方式运行2个SQL“SELECT”? 或者在处理它们之前获得行数的任何其他更好的方法 - How to run 2 SQL “SELECT”s atomically? Or any other better way to get number of rows before i process them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM