views:

45

answers:

3

In SQL Server there is two schemas for metadata:

  • INFORMATION_SCHEMA
  • SYS

I have heard that INFORMATION_SCHEMA tables are based on ANSI standard. When developing e.g. stored procedures, should it be wise to use INFORMATION_SCHEMA tables over sys tables?

+3  A: 

I would always try to use the Information_schema views over querying the sys schema directly.

The Views are ISO compliant so in theory you should be able to easily migrate any queries across different RDBMS.

However, there have been some cases where the information that I need is just not available in a view.

I've provided some links with further information on the views and querying a SQL Server Catalog.

http://msdn.microsoft.com/en-us/library/ms186778.aspx

http://msdn.microsoft.com/en-us/library/ms189082.aspx

Barry
Yes, I agree - as long as you need to / want to be "portable" between databases. If you do 100% of SQL Server work, I typically use the "sys." catalog views, since those are more complete, more concise, and better organized (in my opinion) - full well knowing those queries won't work on DB2 or MySQL...
marc_s
@Marc_s - Yes i should have really made a point of that. If you are **really** sure that it doesn't need to be migrated anywhere else then you'll be fine using the `sys` schema. Nice comment
Barry
+1  A: 

Unless you are writing an application which you know for a fact will need to be portable or you only want quite basic information I would just default to using the proprietary SQL Server system views to begin with.

The Information_Schema views only show objects that are compatible with the SQL-92 standard. This means there is no information schema view for even quite basic constructs such as indexes (These are not defined in the standard and are left as implementation details.) Let alone any SQL Server proprietary features.

Martin Smith
"...you know for a fact will need to be portable..." -- remember that each new version of SQL Server is a port. Future-proofing is a judgement call e.g. are the SQL Server team more likely to ditch the Standard Information_Schema VIEWs or revise their proprietary sys tables?
onedaywhen
+1  A: 

INFORMATION_SCHEMA is more suitable for external code that may need to interface with a variety of databases. Once you start programming in the database, portability kind of goes out the window. If you are writing stored procedures, that tells me you have committed to a particular database platform (for better or for worse). If you have committed to SQL Server, then by all means, use the sys views.

Peter