Where are Programmability Objects Stored in SQL Server?

Where are Programmability Objects Stored in SQL Server?

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0
SQL Server programmability objects such as stored procedures, functions, assemblies, etc. are widely used, especially in cases of a major database design where you want to have a well-structured database with code reuse and performance.

Even though many of us use these programmability features, we do not often think of where these objects are stored.

However, it is very useful to know where the above objects are stored as we can access them via T-SQL and retrieve useful information (i.e. in the case of an upgrade where we want to check for deprecated features in the T-SQL definition of those objects) or even apply corrections.

Taken the above into consideration, there are two catalog views that we need to study prior to see the T-SQL statements that can be used for accessing the programmability objects.

The first catalog is “sys.objects“. This catalog view provides information about user-defined, schema-scoped objects created within a database such as: object name, id, type, creation/modification date, etc. From Books Online we can see that in SQL Server Database Engine there are the following object types available:

  • AF = Aggregate function (CLR)
  • C = CHECK constraint
  • D = DEFAULT (constraint or stand-alone)
  • F = FOREIGN KEY constraint
  • FN = SQL scalar function
  • FS = Assembly (CLR) scalar-function
  • FT = Assembly (CLR) table-valued function
  • IF = SQL inline table-valued function
  • IT = Internal table
  • P = SQL Stored Procedure
  • PC = Assembly (CLR) stored-procedure
  • PG = Plan guide
  • PK = PRIMARY KEY constraint
  • R = Rule (old-style, stand-alone)
  • RF = Replication-filter-procedure
  • S = System base table
  • SN = Synonym
  • SO = Sequence object
  • SQ = Service queue
  • TA = Assembly (CLR) DML trigger
  • TF = SQL table-valued-function
  • TR = SQL DML trigger
  • TT = Table type
  • U = Table (user-defined)
  • UQ = UNIQUE constraint
  • V = View
  • X = Extended stored procedure

The screenshot below displays some of the objects of the “AdventureWorks2012” sample database:

Screenshot 1: Partial set of records retrieved from the sys.objects catalog view in the “AdventureWorks2012” sample database.
The second catalog is “sys.sql_modules“. This catalog view provides information about database objects that are SQL language-defined modules. The most important column in this catalog is “definition” which is of the data type nvarchar(max). The “definition” column contains the SQL text that defines the module. In cases where there is a NULL value it means that it is encrypted.

The screenshot below displays some of the sql_modules of the “AdventureWorks2012” database:

Screenshot 2: Partial set of records retrieved from the sys.sql_modules catalog view in the “AdventureWorks2012” sample database.

Now let’s run some queries by joining the above two system catalogs. For example let’s find the T-SQL definition for all stored procedures in the AventureWorks2012 database:

USE AdventureWorks2012;
GO

SELECT o.name as SPName, m.[Definition],o.create_date as DateCreated, o.type_desc as TypeDescription
FROM sys.objects o, sys.sql_modules m
WHERE o.[object_id]=m.[object_id]
AND o.[type]=’P’;
GO

And this is what we get:

Screenshot 3: All stored procedures of theAdventureWorks2012 sample database.

Now let’s find all the SQL scalar functions for the same database:

USE AdventureWorks2012;
GO

SELECT o.name as SPName, m.[Definition],o.create_date as DateCreated, o.type_desc as TypeDescription
FROM sys.objects o, sys.sql_modules m
WHERE o.[object_id]=m.[object_id]
AND o.[type]=’FN’;
GO

Let’s see the result:

Screenshot 4: All scalar functions of the AdventureWorks2012 sample database.

You can find even more information on different database objects by joining the above catalog views with other such as sys.parameters (finding the parameters of stored procedures), sys.types (finding information about system- and user-defined data types), etc. By the time all this information is available the only limit is your imagination!


Recommended eBooks on SQL Server:

Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Artemakis Artemiou
Artemakis Artemiou is a Senior SQL Server Architect, Author, Software Developer and a Microsoft Data Platform MVP. He has over 15 years of experience in the IT industry in various roles. Among other, via his initiative SQLEBooks.com, Artemakis authors and publishes eBooks on different topics on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Additionally he is the founder of the SQLArtBits initiative that aims to provide the technical community with simple, yet powerful and high-quality SQL Server tools. Currently, the highlights of these tools are DBA Security Advisor and In-Memory OLTP Simulator. Artemakis's official website can be found at aartemiou.com. Artemakis's blogs can be found at: SQLNetHub.com and TechHowTos.com.