Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Truncate source table after copy sql 2005

Posted on 2009-04-08
10
393 Views
Last Modified: 2012-05-06
Hello,
I see all of the advice on how to copy the tables from one db to another...
Here is my situation:
I need to copy (once daily) data from Server1 DB1 Table to Server 2 DB2 Table... Structure is exact (Primary Key is exact). Both are SQL 2005
THEN Truncate the data in DB1 Table.

So, if this is my SP (below) to copy (is it correct?) then I need to know:
How to set it up to run daily
How to Truncate data in DB1 Table.
SELECT * INTO DB2.dbo.Table FROM DB1.dbo.Table (I'll use field names if that is recommeded - cleaner)
0
Comment
Question by:ncrocker
  • 6
  • 3
10 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 24098285
If you're transferring to a different server and don't have a linked server setup, you'll want to use SSIS to do the transfer.  From there, you can create a sql agent job to run the package, and after that step, run a truncate table statement:

truncate table tablename

Does that make sense? you just need a sql agent job to run whatever you need...that way you can schedule it to run daily.
0
 

Author Comment

by:ncrocker
ID: 24098386
Ok, Consider me stupid... How do I know If I have a linked server... I think I do...
I was given a little direction on this and was told that I should:
Get data from DB1 Table Copy into DB2 Table, then truncate... I'm sure I need to (delete) truncate the data on DB1 as it is our live server that doesn't really need this data after the copy.
Where would my truncate table statement live?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24098412
Can you currently transfer data from your source server to your destination server ok?
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

by:ncrocker
ID: 24098460
Yes
0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 20 total points
ID: 24098486
OK, no problem then.  Just use whatever code you have from your first post.  Now you need to create a SQL Job.  Here's an article I wrote that shows how:
http://blogs.techrepublic.com.com/datacenter/?p=296
0
 

Accepted Solution

by:
ncrocker earned 0 total points
ID: 24098576
So,
My SP would look like this:
IF OBJECT_ID('DB1') IS NULL
  BEGIN
      SELECT * INTO DB2.Table
      FROM DB1.Table            

      ALTER TABLE DB1

  END        

 TRUNCATE TABLE DB1.Table  
And Then I'd just schedule the sp?
0
 

Author Comment

by:ncrocker
ID: 24208360
This has not worked.
0
 

Author Comment

by:ncrocker
ID: 24743870
Only part of chapmandew's solution worked; I was able to get the rest to work myself.
0
 

Author Comment

by:ncrocker
ID: 24743874
All points to chapmandew:
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24744492
"as the solution and chapmandew's comment #24098486 (20 points)"
20 points?  That was very generous of you.  Are you sure you can spare those points?
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

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…
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

840 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