Get-DbaDbSequence
View SourceSynopsis
Retrieves SQL Server sequence objects and their configuration details from specified databases.
Description
Retrieves sequence objects from SQL Server databases, returning detailed information about each sequence including data type, start value, increment value, and schema location. Sequences provide a flexible alternative to IDENTITY columns for generating sequential numeric values, allowing values to be shared across multiple tables and offering more control over numbering behavior. This function helps DBAs inventory sequences across databases, verify sequence configurations, and identify sequences that may need maintenance or optimization.
Syntax
Get-DbaDbSequence
[[-SqlInstance] <DbaInstanceParameter[]>]
[[-SqlCredential] <PSCredential>]
[[-Database] <String[]>]
[[-Sequence] <String[]>]
[[-Schema] <String[]>]
[[-InputObject] <Database[]>]
[-EnableException]
[<CommonParameters>]
Examples
Example: 1
PS C:\> Get-DbaDbSequence -SqlInstance sqldev01 -Database TestDB -Sequence TestSequence
Finds the sequence TestSequence in the TestDB database on the sqldev01 instance.
Example: 2
PS C:\> Get-DbaDatabase -SqlInstance sqldev01 -Database TestDB | Get-DbaDbSequence -Sequence TestSequence -Schema TestSchema
Using a pipeline this command finds the sequence named TestSchema.TestSequence in the TestDB database on the sqldev01 instance.
Example: 3
PS C:\> Get-DbaDbSequence -SqlInstance localhost
Finds all the sequences on the localhost instance.
Example: 4
PS C:\> Get-DbaDbSequence -SqlInstance localhost -Database db
Finds all the sequences in the db database on the localhost instance.
Example: 5
PS C:\> Get-DbaDbSequence -SqlInstance localhost -Sequence seq
Finds all the sequences named seq on the localhost instance.
Example: 6
PS C:\> Get-DbaDbSequence -SqlInstance localhost -Schema sch
Finds all the sequences in the sch schema on the localhost instance.
Optional Parameters
-SqlInstance
The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the function
to be executed against multiple SQL Server instances.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | true (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.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-Database
Specifies which databases to search for sequence objects. Accepts wildcards and multiple database names.
Use this when you need to limit the search to specific databases instead of scanning all databases on the instance.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-Sequence
Filters results to sequences with specific names. Accepts multiple sequence names and supports exact name matching.
Use this when you need to find specific sequences across databases rather than retrieving all sequences.
| Property | Value |
|---|---|
| Alias | Name |
| Required | False |
| Pipeline | false |
| Default Value |
-Schema
Filters results to sequences within specific schemas. Accepts multiple schema names for searching across different schemas.
Use this when you need to examine sequences in particular schemas, such as application-specific schemas or custom organizational structures.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-InputObject
Accepts database objects from Get-DbaDatabase pipeline input, allowing you to target specific databases already retrieved.
Use this approach when you need to chain commands or work with databases that meet specific criteria from previous filtering operations.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | true (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.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value | False |
Outputs
Microsoft.SqlServer.Management.Smo.Sequence
Returns one or more Sequence objects from the specified database(s) and schema(s). Each object represents a SQL Server sequence definition with its configuration properties.
Default display properties (via Select-DefaultView):
- ComputerName: The name of the computer where the SQL Server instance is running
- InstanceName: The SQL Server instance name
- SqlInstance: The full SQL Server instance name (computer\instance format)
- Database: The name of the database containing the sequence
- Schema: The schema where the sequence is created
- Name: The name of the sequence object
- DataType: The data type of values the sequence will generate (e.g., bigint, int, tinyint, smallint)
- StartValue: The initial value the sequence will return on first use (the current starting point)
- IncrementValue: The amount the sequence will increase (or decrease if negative) with each NEXT VALUE FOR call
Additional properties available (from SMO Sequence object):
- CurrentValue: The current value that will be returned by the next NEXT VALUE FOR call
- MinValue: The minimum value the sequence can generate
- MaxValue: The maximum value the sequence can generate
- IsCycleEnabled: Boolean indicating whether the sequence will cycle from MaxValue back to MinValue
- CacheSize: The number of sequence values pre-allocated in memory (0 means no cache)
- SequenceCacheType: The cache behavior setting (DefaultCache, NoCache, or CacheWithSize)
- Parent: Reference to the parent Database SMO object
- Urn: The Uniform Resource Name (URN) identifying the sequence in the SMO object hierarchy
- State: The state of the SMO object (Existing, Creating, Altering, Dropping, etc.) All properties from the base SMO Sequence object are accessible even though only default properties are displayed without using Select-Object *.
dbatools