Solved

Cannot drop table because it is being used for replication

Posted on 2011-09-27
7
3,022 Views
Last Modified: 2012-08-14
SERVER 2008
WINDOWS SERVER 2008

I have a database that gets updated every night with new data that I import from an external server.  This database is also replicated each hour to another database which is used by a website.

More detailed ... at 3am each morning I import the new data, drop the existing tables and INSERT INTO a new table, then create new indexes and constraints.  From 3:30am - 6:30 pm, I publish these new tables each hour ... my website database is the subscriber.

Since I set up the replication, I get the error that I cannot drop the table because it is being used with replication.  I am aware that I can rather delete the table contents (rather than drop the table) and insert the new data, but I prefer to drop the tables because it uses much less resources.

MY QUESTION:  Is there a stored procedure or another method that will allow me to simply disable the replication, run my TSQL code that drops and recreates the tables, and then re-enable the replication ... or do I have to actually drop the replication, run my TSQL, then recreate the replication (using TSQL).

Any advice would be very appreciated!

Max
0
Comment
Question by:MaxwellTurner
  • 3
  • 2
  • 2
7 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36711382
If you are just worried about resources, you can TRUNCATE TABLE and DROP the indices.
0
 
LVL 39

Expert Comment

by:lcohan
ID: 36712199
If the table is enabled for replication then unfortunately the answer is yes - you must drop replication first, modifi your table structure, and enable replication again.
I know is a pain however it's the only way it works.
0
 
LVL 1

Author Comment

by:MaxwellTurner
ID: 36712314
@ iCOHAN,

Can you confirm ... In Mgmnt Studio, under the Replication tab I can see my publications.  If I right click on a publication, I can choose Generate Scripts.  On the window that opens I can either choose to create a script to "Disable/Drop Components" or "Enable/Create Components".

Is as simple as creating scripts to Disable/Drop the publisher components and run this before I drop the tables, then run the script to Enable/Create the components after I have dropped/updated the tables?

Btw, I used to DELETE and then INSERT but my database size was becoming to large, so that is why I changed it to DROP and SELECT INTO.  At the same time I also changed the recovery model to simple to help keep the log files smaller ... Will the T-Logs grow larger using DELETE or TRUNCATE as opposed to DROP, or were my log files getting so large mainly because I was using a FULL recovery model before?

Max
0
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.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36712611
TRUNCATE TABLE is different than DELETE.
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/A_2667-The-Difference-Between-Truncation-and-Deletion.html

If the only reason you changed is due to table size and T-Log growth, see information on TRUNCATE. I use it to clear three tables with about 6-10 million rows before running a process that was written to do DELETE and as such run out of space before completion. Running TRUNCATE before, it runs without needing incremental log space.
0
 
LVL 39

Expert Comment

by:lcohan
ID: 36713037
"mwvisa1" is right  with truncate and use that if you can because all DELETE/INSERT rows wil go thrugh DBs t-logs via replication.

http://msdn.microsoft.com/en-us/library/ms177570.aspx

"SQL Server 2008 R2 Other Versions  SQL Server "Denali" SQL Server 2008 SQL Server 2005
Removes all rows from a table without logging the individual row deletions. TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; however, TRUNCATE TABLE is faster and uses fewer system and transaction log resources."
0
 
LVL 1

Author Comment

by:MaxwellTurner
ID: 36713615
Yes this all about controlling the log file size.  I don't care if there are any log files in this database, because it gets re-created each night and is read only ... the data already exists somewhere.

I have changed everything to TRUNCATE TABLE and INSERT INTO, but my log still keeps growing approx 2 GB each time I run the T-SQL job.  I am dealing with mid-hundred thousand records in the half of the tables ... much less in the other half.

How can I control the file size?  I thought that if I used simple recovery and ran a FULL backup, that would truncate the log file ... am I mistaken?  When I was dropping the tables (before replication), my dbase size stayed around 7-10 GB ... now after running the job a couple times, it is already 33 GB.

Max
0
 
LVL 1

Author Comment

by:MaxwellTurner
ID: 36713719
UPDATE:  I was looking at the size of another dbase by mistake ... it seems I am holding steady at 20 GB after running the job a couple more times.  I can live with that!

Thanks for all the helpful info guys.

Max
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 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