commands

^

Set-DbaDbSequence

Author Adam Lancaster, github.com/lancasteradam
Availability Windows, Linux, macOS

 

Want to see the source code for this command? Check out Set-DbaDbSequence on GitHub.
Want to see the Bill Of Health for this command? Check out Set-DbaDbSequence.

Synopsis

Modifies properties of existing SQL Server sequence objects

Description

Modifies existing SQL Server sequence objects by updating their properties such as increment value, restart point, minimum and maximum bounds, cycling behavior, and cache settings. This function is essential when you need to adjust sequence behavior after deployment, fix increment issues, or optimize performance without recreating the sequence and losing its current state.

Syntax

Set-DbaDbSequence
    [[-SqlInstance] <DbaInstanceParameter[]>]
    [[-SqlCredential] <PSCredential>]
    [[-Database] <String[]>]
    [-Sequence] <String[]>
    [[-Schema] <String>]
    [[-RestartWith] <Int64>]
    [[-IncrementBy] <Int64>]
    [[-MinValue] <Int64>]
    [[-MaxValue] <Int64>]
    [-Cycle]
    [[-CacheSize] <Int32>]
    [[-InputObject] <Database[]>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Set-DbaDbSequence -SqlInstance sqldev01 -Database TestDB -Sequence TestSequence -RestartWith 10000 -IncrementBy 10

Modifies the sequence TestSequence in the TestDB database on the sqldev01 instance. The sequence will restart with 10000 and increment by 10.

Example: 2
PS C:\> Get-DbaDatabase -SqlInstance sqldev01 -Database TestDB | Set-DbaDbSequence -Sequence TestSequence -Schema TestSchema -Cycle

Using a pipeline this command modifies the sequence named TestSchema.TestSequence in the TestDB database on the sqldev01 instance. The sequence will now cycle the sequence values.

Required Parameters

-Sequence

Specifies the name of the sequence object to modify. This is the sequence you want to update properties for.
Must be an existing sequence in the specified schema, otherwise the function will fail.

Alias Name
Required True
Pipeline false
Default Value

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.

Alias
Required False
Pipeline false
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.

Alias
Required False
Pipeline false
Default Value
-Database

Specifies the target database containing the sequence to modify. Accepts multiple database names.
Required when using SqlInstance parameter to identify which database contains the sequence.

Alias
Required False
Pipeline false
Default Value
-Schema

Specifies the schema containing the sequence to modify. Defaults to 'dbo' if not specified.
Use this when your sequence exists in a custom schema rather than the default dbo schema.

Alias
Required False
Pipeline false
Default Value dbo
-RestartWith

Sets the next value the sequence will return when NEXT VALUE FOR is called. Immediately resets the sequence to this value.
Use this to fix sequence gaps, realign sequences after data imports, or reset sequences for testing.

Alias
Required False
Pipeline false
Default Value 0
-IncrementBy

Sets how much the sequence value increases (or decreases if negative) with each NEXT VALUE FOR call.
Common values are 1 for sequential numbering or larger values for reserving ranges. Cannot be zero.

Alias
Required False
Pipeline false
Default Value 0
-MinValue

Sets the lowest value the sequence can generate. Once reached, sequence behavior depends on the Cycle setting.
Use this to establish data range constraints or prevent sequences from going below business-required minimums.

Alias
Required False
Pipeline false
Default Value 0
-MaxValue

Sets the highest value the sequence can generate. Once reached, sequence behavior depends on the Cycle setting.
Use this to prevent sequences from exceeding data type limits or business-defined maximum values.

Alias
Required False
Pipeline false
Default Value 0
-Cycle

Enables the sequence to restart from MinValue after reaching MaxValue (or vice versa for negative increments).
Use this for scenarios like rotating through a fixed set of values or when sequences need to wrap around.

Alias
Required False
Pipeline false
Default Value False
-CacheSize

Sets the number of sequence values SQL Server pre-allocates in memory for faster access.
Use 0 to disable caching (guarantees no gaps but slower performance), or specify a number for high-performance scenarios. Omit this parameter to let SQL Server choose an optimal cache size.

Alias
Required False
Pipeline false
Default Value 0
-InputObject

Accepts database objects from Get-DbaDatabase via pipeline to modify sequences across multiple databases.
Use this for batch operations when you need to modify the same sequence in multiple databases.

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.

Alias
Required False
Pipeline false
Default Value False
-WhatIf

Shows what would happen if the command were to run. No actions are actually performed.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

Prompts you for confirmation before executing any changing operations within the command.

Alias cf
Required False
Pipeline false
Default Value