Thor Logo dbatools

Set-DbaDbSequence

View Source
Adam Lancaster, github.com/lancasteradam
Windows, Linux, macOS

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.

PropertyValue
AliasName
RequiredTrue
Pipelinefalse
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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
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 the target database containing the sequence to modify. Accepts multiple database names.
Required when using SqlInstance parameter to identify which database contains the sequence.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Valuedbo
-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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value0
-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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value0
-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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value0
-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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value0
-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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value0
-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.

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
-WhatIf

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

PropertyValue
Aliaswi
RequiredFalse
Pipelinefalse
Default Value
-Confirm

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

PropertyValue
Aliascf
RequiredFalse
Pipelinefalse
Default Value