Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to automate copying data

Author  Topic 

zhtway1
Starting Member

10 Posts

Posted - 2010-08-20 : 01:47:32
Hi,
I am very new to sql server.
I would like to copy data from "table1" to "table2" in the specific time. Which is the best way to do?

Thanks
zhtway

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-20 : 01:48:36
INSERT INTO Table2 (Column1, Column2, ...)
SELECT Column1, Column2, ...
FROM Table1
WHERE SomeColumn ...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

zhtway1
Starting Member

10 Posts

Posted - 2010-08-20 : 01:51:01
Hi,

Thanks..How could I automate it everyday? Which is the best way?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-20 : 01:54:57
You can schedule it to run via a SQL job.

But why do you want to automate it? Why do you need the data in Table2? What version of SQL Server are you using?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

zhtway1
Starting Member

10 Posts

Posted - 2010-08-20 : 02:01:13
Hi,
In table1, I want to collect data for daily activities, then transfer to table2 to save for history. My sql server is 2005
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-20 : 02:05:11
You should consider table partitioning instead then.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

zhtway1
Starting Member

10 Posts

Posted - 2010-08-20 : 02:08:43
Hi again,
Thanks for your reply.
Actually, what I am doing is collection data with mobile device (sql compact server) then sync with sqlserver2005. So those data in sync tables want to store in history table. Is it suitable to use partition table with sync features?
rgds
zhtway
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-20 : 02:11:03
I think zhtway1 is trying to take a history snapshot each night?

table2 will grow large quickly if you copy all rows from Table1 to Table2 every night.

Perhaps you will delete rows from Table2 after a short while? You could insert the Date/Time when the copy was made into Table2 (and use that to delete old/stale rows after a suitable period of time)

An alternative would be to use an Audit table and to store records in that when they change - so rather than copying all records from Table1 to Table2 every day, whether they have changed or not, you could copy them only when they change (and store the date/time of the change). (SQL Server provides a method called a "Trigger" which will automatically "fire" every time a record is inserted or updated (or deleted)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-20 : 02:11:56
Sorry, was posting whilst you were answering
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-20 : 02:13:11
What happens to the original data? Is it deleted? (in which case, in effect, you want to MOVE the data from Table1 to Table2 - so Table1 is empty and can receive more data and Table2 is the permanent record of the data?)
Go to Top of Page

zhtway1
Starting Member

10 Posts

Posted - 2010-08-20 : 02:21:14
Hi Kristen,

Yes, daily data is 200 on mobile sql and will purge every day. History table will store at lest 2 years.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-20 : 07:44:14
Then I think you just want to INSERT INTO TargetTable (Col1, Col2, ...) SELECT Col1, Col2, ... FROM SourceTable and then delete those from the SourceTable.

In SQL 2008 (maybe SQL2005 too) you should be able to use the OUTPUT phrase to delete the records that are successfully inserted, or you could do that as a separate job (preferable with a JOIN to TargetTable to prove that they are actually present before you delete them!
Go to Top of Page

zhtway1
Starting Member

10 Posts

Posted - 2010-08-20 : 11:00:07
Hi,
Yes..you are right.
I want to copy only when the condition is meet.
I have a table with "PickUpID" field and "Status" field.
Want to copy to History table only when ALL "Status" field has "Sign" value for each "PickUpID".
If one record is still other value in "Status", I don't want to copy it yet.

So do I just need to write code for job run as an agent?
I don't actually know about the output phrase. I will do some research
Go to Top of Page
   

- Advertisement -