Thor Logo dbatools

Get-DbaDbStoredProcedure

View Source
Klaas Vandenberghe (@PowerDbaKlaas)
Windows, Linux, macOS

Synopsis

Retrieves stored procedures from SQL Server databases with detailed metadata and filtering options

Description

Retrieves stored procedures from one or more SQL Server databases, returning detailed information including schema, creation dates, and implementation details. This function helps DBAs inventory stored procedures across instances, analyze database objects for documentation or migration planning, and locate specific procedures by name or schema. You can filter results by database, schema, or procedure name, and exclude system stored procedures to focus on user-defined objects. Supports multi-part naming conventions for precise targeting of specific procedures.

Syntax

Get-DbaDbStoredProcedure
    [[-SqlInstance] <DbaInstanceParameter[]>]
    [[-SqlCredential] <PSCredential>]
    [[-Database] <Object[]>]
    [[-ExcludeDatabase] <Object[]>]
    [-ExcludeSystemSp]
    [[-Name] <String[]>]
    [[-Schema] <String[]>]
    [[-InputObject] <Database[]>]
    [-EnableException]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Get-DbaDbStoredProcedure -SqlInstance sql2016

Gets all database Stored Procedures

Example: 2
PS C:\> Get-DbaDbStoredProcedure -SqlInstance Server1 -Database db1

Gets the Stored Procedures for the db1 database

Example: 3
PS C:\> Get-DbaDbStoredProcedure -SqlInstance Server1 -ExcludeDatabase db1

Gets the Stored Procedures for all databases except db1

Example: 4
PS C:\> Get-DbaDbStoredProcedure -SqlInstance Server1 -ExcludeSystemSp

Gets the Stored Procedures for all databases that are not system objects

Example: 5
PS C:\> 'Sql1','Sql2/sqlexpress' | Get-DbaDbStoredProcedure

Gets the Stored Procedures for the databases on Sql1 and Sql2/sqlexpress

Example: 6
PS C:\> Get-DbaDatabase -SqlInstance Server1 -ExcludeSystem | Get-DbaDbStoredProcedure

Pipe the databases from Get-DbaDatabase into Get-DbaDbStoredProcedure

Example: 7
PS C:\> Get-DbaDbStoredProcedure -SqlInstance Server1 -Database db1 -Name schema1.proc1

Gets the Stored Procedure proc1 in the schema1 schema in the db1 database

Example: 8
PS C:\> Get-DbaDbStoredProcedure -SqlInstance Server1 -Name db1.schema1.proc1

Gets the Stored Procedure proc1 in the schema1 schema in the db1 database

Example: 9
PS C:\> Get-DbaDbStoredProcedure -SqlInstance Server1 -Database db1 -Name proc1

Gets the Stored Procedure proc1 in the db1 database

Example: 10
PS C:\> Get-DbaDbStoredProcedure -SqlInstance Server1 -Database db1 -Schema schema1

Gets the Stored Procedures in schema1 for the db1 database

Optional Parameters

-SqlInstance

The target SQL Server instance or instances

PropertyValue
Alias
RequiredFalse
Pipelinetrue (ByValue)
Default Value
-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Database

Specifies which databases to search for stored procedures. Accepts database names and supports wildcards.
Use this when you need to focus on specific databases instead of searching across all databases on the instance.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-ExcludeDatabase

Excludes specified databases from the stored procedure search. Accepts database names and supports wildcards.
Useful when you want results from most databases but need to skip specific ones like development or staging databases.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-ExcludeSystemSp

Excludes system stored procedures from results, showing only user-defined stored procedures.
Use this when you want to focus on custom business logic and avoid the hundreds of built-in SQL Server system procedures.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-Name

Specifies exact stored procedure names to retrieve. Supports two-part names (schema.procedure) and three-part names (database.schema.procedure).
Use this when searching for specific procedures by name rather than browsing all procedures in a database or schema.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Schema

Filters results to stored procedures within the specified schema(s). Accepts multiple schema names.
Useful for organizing results by application area or when working with multi-tenant databases that separate objects by schema.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-InputObject

Accepts database objects from Get-DbaDatabase for pipeline processing.
Use this to chain commands when you need to filter databases first, then retrieve stored procedures from the filtered results.

PropertyValue
Alias
RequiredFalse
Pipelinetrue (ByValue)
Default Value
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with “sea of red” exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this “nice by default” feature off and enables you to catch exceptions with your own try/catch.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse

Outputs

Microsoft.SqlServer.Management.Smo.StoredProcedure

Returns one StoredProcedure object per stored procedure found in the specified databases. Each object represents a single stored procedure, including system and user-defined procedures unless filtered.

Default display properties (via Select-DefaultView):

  • ComputerName: The computer name of the SQL Server instance
  • InstanceName: The SQL Server instance name
  • SqlInstance: The full SQL Server instance name (computer\instance)
  • Database: The database name containing the stored procedure
  • Schema: The schema in which the stored procedure is defined
  • ObjectId: The unique identifier for the stored procedure object (displayed as ID)
  • CreateDate: The date and time when the stored procedure was created
  • DateLastModified: The date and time when the stored procedure was last modified
  • Name: The name of the stored procedure
  • ImplementationType: The implementation type (T-SQL or CLR)
  • Startup: Boolean indicating if the procedure is marked as a startup procedure

Additional properties available (from SMO StoredProcedure object):

  • DatabaseId: The unique identifier of the database containing the procedure
  • IsSystemObject: Boolean indicating if this is a system-defined stored procedure
  • And all other standard SMO StoredProcedure properties (use Select-Object * to see all) When -ExcludeSystemSp is specified, system stored procedures are filtered out and only user-defined procedures are returned.