Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 January SQL Server 2012: Error Msg 3729 Cannot DROP SEQUENCE because it is being referenced by Object

SQL Server 2012: Error Msg 3729 Cannot DROP SEQUENCE because it is being referenced by Object

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

The database Sequence Object introduced in SQL Server 2012 and we can use and assign single Sequence object to multiple tables.

In this post, I am sharing a script to find all dependencies of the SQL Server Sequence object so that we can drop a Sequence object quickly.

Below is a small demonstration, in which I created a Sequence object and assigned to two different tables.

Create a Sequence object:

1
2
3
4
CREATE SEQUENCE dbo.Seq_Sample AS
INT START WITH 1
INCREMENT BY 1 ;
GO

Create two different tables:

1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE TABLE dbo.tbl_FirstSample
(
Rno INT
,Name VARCHAR(255)
)
GO
 
CREATE TABLE dbo.tbl_SecSample
(
EmpID INT
,EmpName VARCHAR(255)
)
GO

Assign Sequence object to both the tables:

1
2
3
4
5
6
7
8
9
ALTER TABLE dbo.tbl_FirstSample
ADD CONSTRAINT seq_tbl_FirstSample_Rno
DEFAULT (NEXT VALUE FOR dbo.Seq_Sample) FOR Rno;
GO
 
ALTER TABLE dbo.tbl_SecSample
ADD CONSTRAINT seq_tbl_SecSample_EmpID
DEFAULT (NEXT VALUE FOR dbo.Seq_Sample) FOR EmpID;
GO

Now, try to drop the Sequence object:

1
2
DROP SEQUENCE dbo.Seq_Sample
GO

You will get a below error:

SQL Server Sequence Error

Script to find and generate Alter command for all the dependencies:

1
2
3
4
5
6
7
8
9
SELECT
'ALTER TABLE '
+ object_name(parent_object_id)
+ ' DROP CONSTRAINT '
+ referencing_entity_name AS AlterCommands
FROM sys.dm_sql_referencing_entities ('dbo.Seq_Sample', 'OBJECT') AS DR
INNER JOIN sys.default_constraints AS DC
ON DR.referencing_id=DC.object_id
GO

The result:

SQL Server Sequence Drop Constraint

Before dropping a Sequence object, we should drop all Sequence object constraint so copy the above result and drop both constraints before dropping a Sequence object.

Jan 27, 2016Anvesh Patel
Database Design: Should we allow NULL or We should apply NOT NULL ?Database Design: Storing a comma separated list in a Database, Is a Bad Practice?
Anvesh Patel

Database Engineer

January 27, 2016 SQL ServerAnvesh Patel, Constraint, database, database research and development, dbrnd, sequence, SQL Query, SQL Server, SQL Server 2012, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....