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.

 

The need to know where are Programmability Objects Stored in SQL Server

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.

 

Theory and SQL Server Catalog Views

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 MS Docs 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

Learn more tips like this! Enroll to our Online Course!

Check our online course titled “Essential SQL Server Development Tips for SQL Developers” (special limited-time discount included in link).

Sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!

Essential SQL Server Development Tips for SQL Developers - Online Course

Learn More


Examples

The screenshot below displays some of the objects of the “AdventureWorks2012” sample database:
Where are programmability objects stored in SQL Server? - Article on SQLNetHub
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:

Where are programmability objects stored in SQL Server? - Article on SQLNetHub
Screenshot 2: Partial set of records retrieved from the sys.sql_modules catalog view in the “AdventureWorks2012” sample database.

T-SQL Script Examples

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:
Where are programmability objects stored in SQL Server? - Article on SQLNetHub
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:

Where are programmability objects stored in SQL Server? - Article on SQLNetHub
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 Online Courses:

 

Check our Related SQL Server Development Articles:

 

Check our online courses!

Check our eBooks!

Subscribe to our YouTube channel!

Subscribe to our newsletter and stay up to date!

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Reference: SQLNetHub (https://www.sqlnethub.com)

© SQLNetHub