• Subscribe
  • Home
  • About
  • Speaking

Dave Turpin, SQL Server Practitioner

What I can forget but want to remember

31 May 10

SQL Server Object_Definition

In SQL Server 2005 the T-SQL meta data function OBJECT_DEFINITION was introduced.  I like to use this command to search stored procedures for string values, such as table or column names.

The BoL page is located at http://msdn.microsoft.com/en-us/library/ms176090.aspx.

The following queries will return the definitions for VIEWS, UDFs and STORED PROCEDURES:

USE AdventureWorks
GO

SELECT    OBJECT_DEFINITION(OBJECT_ID(N'Purchasing.vVendor'))
UNION
SELECT    OBJECT_DEFINITION(OBJECT_ID(N'HumanResources.uspUpdateEmployeeHireInfo'))
UNION
SELECT    OBJECT_DEFINITION(OBJECT_ID(N'dbo.ufnGetContactInformation'));

USE master;
GO

SELECT    OBJECT_DEFINITION(OBJECT_ID(N'INFORMATION_SCHEMA.TABLE_CONSTRAINTS'))
UNION
SELECT    OBJECT_DEFINITION(OBJECT_ID(N'sys.sp_add_log_file_recover_suspect_db'));

The only reason I UNIONed the queries together was to create a more compact result set:

image

In theory you could paste the result set of one of the object definitions into a QUERY window and see how the object is created.  In practice, because the output does not recognize line feeds / carriage returns, you would need to add your own CRs to get around the in-line comments.

This function should be used with the understanding that just because an object definition returns NULL does not mean that the object may not exist.  The BoL indicate that you simply may not have permissions to read that object.  In his book Microsoft SQL Server 2008: T-SQL Fundamentals, T-SQL guru (I know, too oft used, but in this case it really applies) Itzik Ben-Gan shows us that an encrypted VIEW will always return a NULL value.  Lesson Learned: Do not use the OBJECT_DEFINITION meta function to test if an object EXISTS.

SQL Server MVP Dave Pinal shows us in his blog how to use the OBJECT_DEFINITON function to search through sys.procedures (stored procedures) for a certain text string.  I have found this very handy to search for references to tables and columns, for example, in production stored procedures.

USE AdventureWorks
GO
--Searching for Empoloyee table
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Employee%'

image

Stay curious; keep learning…

Dave

0 Comments
Filed in T-SQL
  Posted by Dave
Tagged meta data functions, object_definition, sys.procedures

Leave a Reply

You must be logged in to post a comment.

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>

Dave Turpin, SQL Server Practitioner is powered by WordPress using theme Tribune
  • Archives

    • February 2012 (1)
    • January 2012 (2)
    • October 2011 (1)
    • July 2011 (3)
    • April 2011 (1)
    • March 2011 (3)
    • January 2011 (1)
    • November 2010 (3)
    • October 2010 (1)
    • September 2010 (2)
    • August 2010 (1)
    • July 2010 (2)
    • June 2010 (1)
    • May 2010 (2)
    • April 2010 (7)
    • March 2010 (3)
    • February 2010 (4)
    • January 2010 (2)
    • December 2009 (2)
    • November 2009 (3)
    • October 2009 (13)
    • September 2009 (3)
  • Blog Stats

    163461 Readers

  • Blogroll

    • Amazing FREE Webcasts on Perf Tuning
    • Blog: Query processing, execution and plans
    • Charlote SQL Saturday 2010
    • Charlotte SQL Server UG
    • Itzik Ben-Gan, et al at SQL Mag
    • Microsoft SQL Query Processing Team
    • SQL Bits
    • SQL Server CSS
    • SQLBlog.com
    • SQLCAT Customer Advisory Team
  • Tags

    $partition 7-zip AGGREGATE transformation ALTER DATABASE ALTER TABLE Career Development CHECKSUM compound assignment operator count(*) create database CREATE PARTITION FUNCTION CREATE PARTITION SCHEME CTE dbcc derived column dropcleanbuffers EXCEPT SET OPERATOR FLAT FILE DESTINATION freeproccache hashing HTTP IDENTITY Indexed View INSERT isnumeric msdb NCOA NOLOCK hint Recursion restore database row count set statistics snapshots sp_helpdb sp_start_job sql agent SSIS sys.databases syscolumns sysindex system table TABLESAMPLE TOP TRANSACTION UPDATE
  • For Official Use Only

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • New Downloads

    • Table List by Size
    • Filegroups and Physical Files
    • Database Level Report
    • Backup Snapshots
    • Instance Level Report
    • Update Stats
    • SQL Saturday 20100306 Query Plans