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!
Examples
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!
Recommended Online Courses:
- AI Essentials: A Beginner’s Guide to Artificial Intelligence
- SQL Server 2022: What’s New – New and Enhanced Features
- Working with Python on Windows and SQL Server Databases
- Introduction to Azure Database for MySQL
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- Data Management for Beginners – Main Principles
- A Guide on How to Start and Monetize a Successful Blog
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
- Sequence Objects in SQL Server 2012 and later
- …more
Check our online courses!
Check our eBooks!
Subscribe to our YouTube channel!
Subscribe to our newsletter and stay up to date!
Rate this article:
Reference: SQLNetHub (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.