Solved

Drop SQL Server 2005 replication columns

Posted on 2010-08-26
2
868 Views
Last Modified: 2012-05-10
Hi,

I have a database in SQL Server 2005, which has the column 'rowguid' in each table. This is due to replication.

I need to delete all the rowguid columns from all tables and associated defaults.
Below is the code I am using, but I get the following error:

Msg 5074, Level 16, State 1, Line 1
The index 'index_5575058' is dependent on column 'rowguid'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE DROP COLUMN rowguid failed because one or more objects access this column.

Thank you
/**************************************************************/

--DROP REPLICATION

/**************************************************************/



-- Clear All Defaults

DECLARE defaults CURSOR FOR

SELECT name,parent_obj FROM sysobjects WHERE name like 'DF%' and name like '%_rowgu_%'



OPEN defaults

DECLARE @Name varchar(1000)

DECLARE @TableName varchar(1000)

DECLARE @ParentObj int



FETCH NEXT FROM defaults into @Name,@ParentObj

WHILE @@FETCH_STATUS <> -1

BEGIN

	SELECT @TableName = (SELECT name FROM sysobjects WHERE id=@ParentObj)

	exec('ALTER TABLE [' + @TableName + '] DROP CONSTRAINT [' + @Name + ']')

	FETCH NEXT FROM defaults into @Name,@ParentObj

END



CLOSE defaults

DEALLOCATE DEFAULTS



GO 



--now clear all tables

DECLARE cols CURSOR FOR

SELECT name,id FROM syscolumns WHERE PATINDEX('rowguid%',name)>0 AND id IN(SELECT id FROM sysobjects WHERE xtype='U')



OPEN cols

DECLARE @Name varchar(1000)

DECLARE @TableName varchar(1000)

DECLARE @ParentObj int



FETCH NEXT FROM cols into @Name,@ParentObj

WHILE @@FETCH_STATUS <> -1

BEGIN

	SELECT @TableName = (SELECT name FROM sysobjects WHERE id=@ParentObj)

	exec('ALTER TABLE [' + @TableName + '] DROP COLUMN [' + @Name + ']')

	FETCH NEXT FROM cols into @Name,@ParentObj

END



CLOSE cols

DEALLOCATE cols



GO

Open in new window

0
Comment
Question by:crompnk
2 Comments
 
LVL 10

Accepted Solution

by:
dwe761 earned 500 total points
ID: 33531384
0
 

Author Closing Comment

by:crompnk
ID: 33575517
ok
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now