SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete constraints - SQL Server 2005


Delete constraints - SQL Server 2005

Author
Message
sqldummy79
sqldummy79
SSC Veteran
SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)

Group: General Forum Members
Points: 287 Visits: 237
Comments posted to this topic are about the item Delete constraints - SQL Server 2005



timothyawiseman
timothyawiseman
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3412 Visits: 920
Thank you. This saved me the trouble of writing something like it.

Incidentally, it does not get rid of defaults if you consider those constraints.

---
Timothy A Wiseman
SQL Blog: http://timothyawiseman.wordpress.com/
Larry Leonard-399461
Larry Leonard-399461
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 534
Try this:


USE MyDB

DECLARE @sSql NVARCHAR(4000) SET @sSql = ''
DECLARE @sTypeDesc sysname SET @sTypeDesc = ''
DECLARE @sTableName sysname SET @sTableName = ''
DECLARE @sConstraintName sysname SET @sConstraintName = ''

DECLARE cur CURSOR DYNAMIC FOR
SELECT name,
type_desc,
OBJECT_NAME(parent_object_id)
FROM sys.objects
WHERE is_ms_shipped = 0
AND type_desc LIKE '%_CONSTRAINT'
ORDER BY type_desc, name

WHILE 1 = 1
BEGIN
OPEN cur
FETCH FIRST FROM cur INTO @sConstraintName, @sTypeDesc, @sTableName

IF @@FETCH_STATUS != 0
BEGIN
CLOSE cur
BREAK
END

WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
SET @sSql = 'ALTER TABLE ' + QUOTENAME(@sTableName) + ' DROP CONSTRAINT ' + QUOTENAME(@sConstraintName)
RAISERROR('%s', 10, 1, @sSql) WITH NOWAIT
EXEC sp_executesql @sSql
END TRY
BEGIN CATCH
DECLARE @ENumber INT SET @ENumber = ISNULL(ERROR_NUMBER(), -1)
DECLARE @ESeverity INT SET @ESeverity = ISNULL(ERROR_SEVERITY(), -1)
DECLARE @EState INT SET @EState = ISNULL(ERROR_STATE(), 0) IF @EState = 0 SET @EState = 42
DECLARE @EProcedure NVARCHAR(126) SET @EProcedure = ISNULL(ERROR_PROCEDURE(), N'{N/A}')
DECLARE @ELine INT SET @ELine = ISNULL(ERROR_LINE(), -1)
DECLARE @EMessageRecv NVARCHAR(2048) SET @EMessageRecv = ISNULL(ERROR_MESSAGE(), N'')
DECLARE @EMessageSent NVARCHAR(440) SET @EMessageSent = N''

IF ERROR_PROCEDURE() IS NOT NULL SET @EMessageSent = N'Error %d, Level %d, State %d, Procedure %s, Line %d, Message: '
SET @EMessageSent = @EMessageSent + ERROR_MESSAGE()
RAISERROR(@EMessageSent, 10 /* @ESeverity */ , @EState, @ENumber, @ESeverity, @EState, @EProcedure, @ELine) WITH LOG
END CATCH

FETCH FIRST FROM cur INTO @sConstraintName, @sTypeDesc, @sTableName
END

CLOSE cur
END

DEALLOCATE cur
GO


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search