2

I need to retrieve information from SQL Server from structure point of view

example: I need to list all database, all procedures, all parameters from procedures, all column name from tables, column type from table columns.

How would be the approach to do that?

thanks

2 Answers 2

3

There are (at least) three ways.

  1. Use the DatabaseMetaData class. There's a lot in there, so for details see the Java Docs. I believe this is the preferred way, as it is standard across all database engines. So if you switch to a different database engine, it shoudl still work, and what you learn about how to use it will apply whether you are working on this db engine or another.

  2. Do queries against the "information_schema". Basically this is a database with information about the database structure. It has tables listing all the tables, all the columns, etc. This is the second-best choice. The information_schema is more or less standard across databases. Unfortunately, the implementation for MS SQL Server is not the best.

  3. Do queries against the "system tables" and/or "system views". There are a set of tables in MS SQL Server with information about the database structure. Like information_schema, but it's specific to MS SQL Server and thus includes metadata for things that are not part of standard SQL, like calculated fields and identity columns. It's also more complete and reliable than MS's implementation of information_schema.

For really general stuff, like what tables are in the DB or what columns are in a given table, information_schema and the system tables are equally good and useful. But if you need much of anything else, MS's implementation of the information_schema quickly proves inadequate. I've seen lots of comments on the Web encouraging developers to use the system tables rather than the information_schema. I don't like to use non-standard stuff, as both the code itself and the experience gained are not portable, but in this case, I think they're right: use the system tables.

But DatabaseMetaData is better yet, if it gives you what you need.

Sign up to request clarification or add additional context in comments.

Comments

2

check out DatabaseMetaData Interface

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.