Solved

Truncate source table after copy sql 2005

Posted on 2009-04-08
10
400 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
[X]
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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

623 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