Where are Programmability Objects Stored in SQL Server?

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:


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!

Labels: ,