Thursday, November 21, 2013

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:

  • 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!
[Ad]
Easily secure your SQL Server instances! Try out DBA Security Advisor!
DBA Security Advisor for SQL Server by SQLArtBits
DBA Security Advisor was developed by SQLArtBits. The tool was being carefully developed for an entire year and it contains comprehensive security checks and recommendations based on proven SQL Server security best practices. Feel free to download and use the Community Edition which is free and if you find it useful, you can consider upgrading to the Enterprise Edition!

Artemakis Artemiou [MVP]

Author & Editor

Artemakis Artemiou is a Senior SQL Server Architect, Software Developer and Microsoft Data Platform MVP. He is also an author, regular blogger, president of Cyprus.NET User Group and CY Country leader of INETA-EU. He is also the creator of DBA Security Advisor and In-Memory OLTP Simulator. Artemakis is a frequent guest author of worldwide well-respected online journals where he writes articles focusing on many SQL Server topics.

Reference: The SQL Server and .NET Hub (http://www.sqlnethub.com)

0 comments: