Fix “START value (…) cannot be greater than MAXVALUE (…)” When Creating a Sequence in PostgreSQL

If you’re getting an error that reads something like “START value (101) cannot be greater than MAXVALUE (100)” in PostgreSQL when you’re trying to create a sequence, it’s probably because your sequence’s maximum possible value is lower than the start value.

To fix this issue, change either the start value or maximum value so that the maximum value is greater than the start value.

Continue reading

6 Ways to Fix “nextval: reached maximum value of sequence” in PostgreSQL

If you’re getting an error that reads something like “nextval: reached maximum value of sequence “sequence1” (3)” in PostgreSQL, it’s probably because you’re trying to generate a new value from a sequence, but the sequence has already reached its maximum value.

We have quite a few options when it comes to dealing with this issue.

Continue reading

Add a Column that Cycles Through a Range of Numbers in a Result Set in SQL Server

If we ever need to assign repeating sequence numbers to a result set in SQL Server, we can use a sequence object with the appropriate arguments.

In particular, we can use the CYCLE argument to specify that the sequence will cycle through the range of numbers. In other words, once it reaches the end of the sequence, it starts again, and will continue to reiterate every time it completes the sequence.

Continue reading

Fix “lastval is not yet defined in this session” When Calling the LASTVAL() Function in PostgreSQL

If you’re getting an error that reads something like “lastval is not yet defined in this session” when calling the lastval() function in PostgreSQL, it’s probably because nextval() hasn’t yet been called in the current session.

If you want to avoid this error, only call the lastval() function when you know that nextval() has been called at least once in the current session.

Continue reading

2 Ways to Set a Maximum and/or Minimum Value when Creating a Sequence in SQL Server

By default, SQL Server sets its own minimum and maximum values for sequence objects based on the data type of the sequence object. Assuming we don’t set the data type for the sequence, these min/max values are based on the bigint data type (because that’s the default data type for sequence objects in SQL Server).

However, we can also set our own minimum and maximum values for our sequences. We can do this either explicitly (by setting the MAXVALUE and MINVALUE properties) or implicitly (by setting the data type).

Continue reading

Fix “START value (…) cannot be less than MINVALUE (…)” When Creating a Sequence in PostgreSQL

If you’re getting an error that reads something like “START value (0) cannot be less than MINVALUE (1)” in PostgreSQL when you’re trying to create a sequence, it’s because your sequence’s start value is lower than its minimum value, when it should be at least the same or higher.

To fix this issue, be sure that the sequence’s start value is at least the same or greater than the minimum value.

Continue reading

Fix “Argument ‘AS’ cannot be used in an ALTER SEQUENCE statement.” (Error Msg 11711) in SQL Server

If you’re getting an error that reads “Argument ‘AS’ cannot be used in an ALTER SEQUENCE statement.” in SQL Server, it’s probably because you’re trying to change the data type of a sequence object.

We can’t actually change the data type of a sequence object, so this error is to be expected.

If you need to change the data type of a sequence object, you’ll need to drop the sequence and recreate it with the correct data type.

Continue reading

How to Create Decrementing Sequence Numbers in SQL Server

When we create a sequence in SQL Server, we have the option of making it an incrementing sequence or decrementing.

By “decrementing”, I mean that the sequence decreases instead of increases. For example, if it starts at 100, the next value is 99, and then 98, and so on.

To create a sequence that decrements, all we do is provide a negative value for the INCREMENT BY argument.

Continue reading