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
The screenshot below displays some of the sql_modules of the “AdventureWorks2012” 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
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:
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!
Check our Related SQL Server Development Articles:
- The set identity_insert Command in SQL Server
- The Import Flat File Wizard in SSMS v17.3
- Listing all Tables of a Linked Server’s Database
- How to Import and Export Unstructured Data in SQL Server – FileTables
- How to Import and Export Unstructured Data in SQL Server – FILESTREAM
- How to Import and Export Unstructured Data in SQL Server – The IMAGE Datatype
Subscribe to our newsletter and stay up to date!
Check out our latest software releases!
Check out Artemakis’s eBooks!
Rate this article:
Reference: SQLNetHub (https://www.sqlnethub.com)