---
title: "Copy one table to another in sql server automatically"  
description: "Copy one table to another in sql server automatically"  
author: "Royce Roy"  
published: 2013-01-17  
updated: 2013-01-29  
canonical: https://www.mindstick.com/forum/538/copy-one-table-to-another-in-sql-server-automatically  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Copy one table to another in sql server automatically

Hello Guys\
\
I have [two tables](https://www.mindstick.com/forum/159646/how-to-join-two-tables-in-oracle-to-get-single-line-results) in same [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) and I want to [copy](https://www.mindstick.com/forum/161993/explain-the-numpy-array-copy-vs-view-with-example) first [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap)'s [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) into [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) table automatically after some interval of time using c#. \
\
Thanks!

## Replies

### Reply by AVADHESH PATEL

Hi Royce Roy!

You can try this line of code

Step 1. Used timer control as below code. For Timer control include **System.Timers** namespace

timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);\
timer1.Interval = 10000;\
timer1.Enabled = true;\
timer1.Start();

2. Create Timer's **Elapsed** event and write code within this event for copy data from one table to another after 10 second (10000 mili seconds = 10 seconds)

private void timer1_Elapsed(object sender, EventArgs e)\
{\
SqlConnection con = new SqlConnection("Data Source = Your database service name; Initial Catalog = WindowsServiceDemo; User Id= server ; Password= password;");\
SqlCommand cmd1;\
SqlCommand cmd2;\
SqlDataAdapter adap = new SqlDataAdapter("Select [EmpID],[EmpName] From [dbo].[EmployeeStatus] Where [IsActive]='False'", con);\
DataTable dt = new DataTable();\
adap.Fill(dt);\
con.Open();\
foreach (DataRow dr in dt.Rows)\
{\
cmd1 = new SqlCommand("Insert into [dbo].[xEmployeeStatus]([EmpID],[EmpName]) values('" + dr[0].ToString() + "','" + dr[1].ToString() + "')", con);\
if (cmd1.ExecuteNonQuery() > 0)\
{\
cmd2 = new SqlCommand("Update [dbo].[EmployeeStatus] Set [IsActive] = 'True' Where [IsActive]='False'", con);\
int i = cmd2.ExecuteNonQuery();\
}\
}\
con.Close();\
dt.Dispose();\
adap.Dispose();\
}


---

Original Source: https://www.mindstick.com/forum/538/copy-one-table-to-another-in-sql-server-automatically

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
