Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « How to call a user defined function with a default parameterHow to capture the error output from a stored procedure when calling another stored procedure in SQL Server? »
    comments

    In my Are you ready for SQL Server 2012 or are you still partying like it is 1999? post, I wrote about how you should start using SQL Server 2005 and SQL Server 2008 functionality now in order to prepare for SQL Server 2012. I still see tons of code that is written in the pre 2005 style and people still keep using those functions, procs and statements even though SQL Server 2005 and 2008 have much better functionality.

    With the introduction of SQL Server 2005 system tables have been deprecated, there are three new set of objects to replace system tables: Compatibility Views, Dynamic Management Views and Catalog Views.

    Compatibility views replace the system tables, so while you still can use syscolumns, you should get into the habit of using sys.columns instead

    Every database

    The following table maps the system tables or functions that live in every database in SQL Server 2000 to their equivalent system views or functions in SQL Server 2008.

    System table or function System view or function Type of view or function
    fn_virtualfilestats sys.dm_io_virtual_file_stats Dynamic management view
    syscolumns sys.columns Catalog view
    syscomments sys.sql_modules Catalog view
    sysconstraints sys.check_constraints Catalog view
    sys.default_constraints Catalog view
    sys.key_constraints Catalog view
    sys.foreign_keys Catalog view
    sysdepends sys.sql_expression_dependencies Catalog view
    sysfilegroups sys.filegroups Catalog view
    sysfiles sys.database_files Catalog view
    sysforeignkeys sys.foreign_key_columns Catalog view
    sysindexes sys.indexes Catalog view
    sys.partitions Catalog view
    sys.allocation_units Catalog view
    sys.dm_db_partition_stats Dynamic management view
    sysindexkeys sys.index_columns Catalog view
    sysmembers sys.database_role_members Catalog view
    sysobjects sys.objects Catalog view
    syspermissions sys.database_permissions Catalog view
    sys.server_permissions Catalog view
    sysprotects sys.database_permissions Catalog view
    sys.server_permissions Catalog view
    sysreferences sys.foreign_keys Catalog view
    systypes sys.types Catalog view
    sysusers sys.database_principals Catalog view
    sysfulltextcatalogs sys.fulltext_catalogs Catalog view


    Master database

    The following table maps the system tables inside the master database in SQL Server 2000 to their equivalent system views or functions in SQL Server 2008.

    System table System views or functions Type of view or function
    sysaltfiles sys.master_files Catalog view
    syscacheobjects sys.dm_exec_cached_plans Dynamic management view
    sys.dm_exec_plan_attributes () Dynamic management view
    sys.dm_exec_sql_text Dynamic management view
    sys.dm_exec_cached_plan_dependent_objects Dynamic management view
    syscharsets sys.syscharsets Compatibility view
    sysconfigures sys.configurations Catalog view
    syscurconfigs sys.configurations Catalog view
    sysdatabases sys.databases Catalog view
    sysdevices sys.backup_devices Catalog view
    syslanguages sys.syslanguages Compatibility view
    syslockinfo sys.dm_tran_locks Dynamic management view
    syslocks sys.dm_tran_locks Dynamic management view
    syslogins sys.server_principals Catalog view
    sys.sql_logins
    sysmessages sys.messages Catalog view
    sysoledbusers sys.linked_logins Catalog view
    sysopentapes sys.dm_io_backup_tapes Dynamic management view
    sysperfinfo sys.dm_os_performance_counters Dynamic management view
    sysprocesses sys.dm_exec_connections Dynamic management view
    sys.dm_exec_sessions Dynamic management view
    sys.dm_exec_requests Dynamic management view
    sysremotelogins sys.remote_logins Catalog view
    sysservers sys.servers Catalog view



    As you can see in the tables above, the sysobjects system table maps to sys.objects. In SQL Server 2005 we actually have a couple of views that take parts of sysobjects, below are these views

    sys.tables
    sys.procedures
    sys.views
    sys.triggers

    As you can see, you now have a view for just tables, triggers, procedures and views.


    Some examples

    Although Dynamic Management Views are listed in the tables above, I will not give examples in this post, I will have another post in this series focusing just on Dynamic Management Views.

    If you wanted a list of all stored procedures in SQL Server 2000, you would typically do something like the following

    1. SELECT name
    2. FROM sysobjects
    3. WHERE type = 'P'

    In SQL Server 2005 and up, you now have a view just for procedures

    1. SELECT name
    2. FROM sys.procedures

    If you run those two queries you might get a different number of rows back, this is because the sys.procedures view has these 4 kinds of procedures

    • P = SQL Stored procedure
    • X = Extended stored procedure
    • RF = Replication-filter-procedure
    • PC = Assembly (CLR) stored-procedure

    In order to make them the same you would do

    1. SELECT name FROM sysobjects
    2. WHERE type IN ( 'P','PC','RF','X')

    That is the same as

    1. SELECT name
    2. FROM sys.procedures

    A quick count of all the procs grouped by type

    1. SELECT COUNT(*), type_desc
    2. FROM sys.procedures
    3. GROUP BY type_desc

    A nice thing about these new views is that they have creation as well as modified dates, you can quickly see if a proc was ever modified or not

    1. SELECT name,
    2.     CASE WHEN create_date = modify_date
    3.             THEN 'Never Modified'
    4.         ELSE 'Modified' END AS modified
    5.  FROM sys.procedures

    Or if you just want a list of procedures that have never been modified (by way of an alter statement, not a drop and a create of course)

    1. SELECT name
    2.  FROM sys.procedures
    3. WHERE create_date = modify_date




    Let's look at some other examples. The sys.messages view in the master database is a replacement for the sysmessages system table. If you run these 2 queries which bring back the same error message you will notice something interesting.

    1. SELECT *
    2. FROM master..sysmessages
    3. WHERE error= 1481
    4. AND msglangid = 1033
    5.  
    6.  
    7. SELECT *
    8. FROM master.sys.messages
    9. WHERE  message_id =1481
    10. AND language_id = 1033

    output
    ---------------------

    Database mirroring could not repair physical page %S_PGID in database "%.*ls". The mirroring partner could not be contacted or did not provide a copy of the page. Possible reasons include a lack of network connectivity or that the copy of the page kept by

    Database mirroring could not repair physical page %S_PGID in database "%.*ls". The mirroring partner could not be contacted or did not provide a copy of the page. Possible reasons include a lack of network connectivity or that the copy of the page kept by the partner is also corrupted. To learn whether the partners are currently connected, view the mirroring_state_desc column of the sys.database_mirroring catalog view. If they are connected, for information about why the partner could not provide a copy of the page, examine its error log entries from around the time when this message was reported. Try to resolve the error and resume mirroring.

    As you can see the text is truncated to 255 characters in the old system views, in the new view you see the whole message.

    I only showed you examples for two of these tables, if I did all of them this post would take forever to write. You can investigate the other tables by yourself, maybe pick one or two a day and mess around with some queries.

    I would say, take a look at these views, start using them, learn them well because they will help you do your job better.

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    1218 views
    Instapaper

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)