Fix “Cannot DROP SEQUENCE ‘…’ because it is being referenced by object” in SQL Server

If you’re getting an error that reads something like “Cannot DROP SEQUENCE ‘Sequence2’ because it is being referenced by object ‘DF__Dogs__DogId__6C190EBB’“, it could be because you’re trying to drop a sequence object that’s referenced by a DEFAULT constraint.

Sequences that are referenced by a DEFAULT constraint can’t be dropped until the DEFAULT constraint is dropped.

Therefore, to fix this issue, drop the DEFAULT constraint first.

Example of Error

Here’s an example of code that produces the error:

DROP SEQUENCE Sequence2;

Result:

Msg 3729, Level 16, State 1, Line 1
Cannot DROP SEQUENCE 'Sequence2' because it is being referenced by object 'DF__Dogs__DogId__6C190EBB'.

I’m trying to drop a sequence called Sequence2, but it is being referenced by a DEFAULT constraint called DF__Dogs__DogId__6C190EBB.

Solution

To fix this issue, we need to drop the DEFAULT constraint before dropping the sequence:

ALTER TABLE Dogs 
DROP CONSTRAINT DF__Dogs__DogId__6C190EBB;

Result:

Commands completed successfully.

Now we can go ahead and drop the sequence:

DROP SEQUENCE Sequence2;

Result:

Commands completed successfully.

The sequence object has now been dropped.