• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

count number of coulmns in table?

 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to get count of number of coulmns in a table in sql query?
 
Lester Burnham
Rancher
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.sql.Connection.getMetaData() is the DB-independent way to do this.
 
Pravin Shirke
Ranch Hand
Posts: 150
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi kaustubh,
create a ResultSetMetaData object on resultset and get the column count of your sql query. like this



where rs is the resultset object

Hope this helps
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir I want this is sql query not on java side...
 
Lester Burnham
Rancher
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you need to consult the documentation of whatever DB you're using. There's no standard for that.
 
Sandeep Sanaboyina
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which database are you using..???
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Microsoft SQL Server 2005
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaustubh G Sharma wrote:sql 2005

You mean Microsoft SQL Server 2005 ?
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes sir
 
Sandeep Sanaboyina
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaustubh G Sharma wrote:Microsoft SQL Server 2005
No experience in SQL Server

Googled it though.. found the below link. Check post 11 in it. Hope it helps.

http://blog.sqlauthority.com/2007/01/10/sql-server-query-to-find-number-rows-columns-bytesize-for-each-table-in-the-current-database-find-biggest-table-in-database/
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I search for ms sql server column count the sixth hit is http://forums.devx.com/archive/index.php/t-25101.html. This shows you how to do it.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks rob well I found this idea before not found any single way out to solve the problem....or atleast possible please let me knew how to do it in "derby" will be very greatful...
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, are you looking for a way that will work across all databases? Lester has already pointed you at the solution for that.

 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Come to know that we don't have any standard for all the databases for the above query but want to know the solution for "derby"
 
Lester Burnham
Rancher
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Substituting "derby" for "ms sql server" in the search Rob mentioned would be the obvious first step to try; have you done that?
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup I already did that..
 
Lester Burnham
Rancher
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the problem is solved now?
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Sir I want the same thing for derby....
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the documentation not tell you this? Given your responses so far I'm not sure anyone here knows. You are probably going to get your answer a bit faster if you check there.
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me make it clear:
There is no query that will work on all database systems.
Sorry that I made it so big, but the message didn't seem to get through.

So you have two options:
- use a different query for all database systems you want to support (using Google initially to find those queries)
- follow Lester's advice: check out DatabaseMetaData; you can get an instance using Connection.getMetaData(). There is one method that returns a ResultSet for all columns. You then only need to go through the ResultSet, incrementing a counter for each result:
Make sure to close() the ResultSet when done.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:There is no query that will work on all database systems.

There is no such query... I say that for people who could take it out of its context.
 
Lester Burnham
Rancher
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:I say that for people who could take it out of its context.

You must have ... interesting ... people at your workplace
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no query that will work on all database systems.


sad to here that....


No Solution found for DERBY as well...
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaustubh G Sharma wrote:

No Solution found for DERBY as well...


Did you check their manual? Specifically, did you check their documentation on system tables? Based on that documentation, I can see that it's possible to find the column count. You'll need admin privileges to query those system tables.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks jaikiran your link really help me out to solve the problem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic