So what are SQL modules? Simply put, SQL modules are objects in the database that are programmed in SQL. This isn't entirely accurate, as there are a few SQL programmable object types that don't fall under the 'sql module' banner. These are:
So, effectively, that leaves us with stored procedures, replication filter procedures, the three types of functions (scalar, in-line table-valued and multi-statement table-valued) as well as both DML and DDL triggers. Also, the legacy 'standalone rule' and 'standalone default' object definitions count as SQL modules. However, the less said about them the better, frankly! The meta-data for these objects is stored in the system view [sys].[sql_modules] - so let's look at what we're offered:
[sys].[sql_modules]
object_id
varchar(MAX)
definition
sp_rename
WITH ENCRYPTION
uses_ansi_nulls
SET ANSI_NULLS
uses_quoted_identifier
SET QUOTED_IDENTIFIER
is_schema_bound
WITH SCHEMABINDING
uses_database_collation
is_recompiled
WITH RECOMPILE
null_on_null_input
execute_as_principal_id
WITH EXECUTE AS
WITH EXECUTE AS CALLER
EXECUTE AS OWNER
EXECUTE AS [database_principal_name]
EXECUTE AS SELF
[sys].[database_principals]
So we get nearly enough to create the script for all SQL modules from this one view. The major exception being the correction of object names for sql modules that have had their name changed using sp_rename. Interestingly, if a procedure is altered using ALTER PROCEDURE, then the definition in [sys].[sql_modules] will still contain the CREATE keyword.
ALTER PROCEDURE
CREATE
About system stored procedures
System stored procedures are not magic, they are just SQL modules. Their defintion can be found in [sys].[all_sql_modules], in exactly the same format as for [sys].[sql_modules]. Ok, so they are slightly magic - they often reference internal objects that you simply don't have access to as a normal SQL user. But, looking at the source of system stored procedures can be a great way to learn more about the internals of SQL server, as well as giving you the basis for creating your own customised versions.
[sys].[all_sql_modules]
Next time we'll look at some of the extra meta-data that is available around SQL modules, specifically triggers, stored procedures and replication filter procedures.
Tags: metadata, stored-procedures, functions, triggers, views
metadata
Related posts
Add comment