views:

47

answers:

3

I have 2 databases on SQL (identical). I want a table from one database to be copied to the other database. They are both the same name and the original table can be overeritten.

Cheers

+1  A: 
DELETE FROM `target_database`.`table_name`

INSERT INTO `target_database`.`table_name` SELECT * FROM `source_database`.`table_name`
Hammerite
Don't forget schema should be 'database'.'schema'.'table'
rdkleine
TRUNCATE `target_database`.`table_name` would be quicker than DELETE FROM.
Mark Bannister
DELETE FROM 'SixthSenseUsers','college_survey_questions'INSERT INTO 'SixthSenseUsers','college_survey_questions' SELECT * FROM 'test','college_survey_questions' ----- I put this in but get an error : --- Server: Msg 170, Level 15, State 1, Line 1Line 1: Incorrect syntax near 'SixthSenseUsers'.Server: Msg 170, Level 15, State 1, Line 2Line 2: Incorrect syntax near 'test'.
Scott Jackson
@Scott - That looks like a Microsoft SQL Server error message? I think Hammerite's answer is using MySQL backticks. You should always state the product that you are using. You don't need any quotes around the object names in that query.
Martin Smith
Its SQL server 2000
Scott Jackson
Try `DELETE FROM SixthSenseUsers.college_survey_questions INSERT INTO SixthSenseUsers.college_survey_questions SELECT * FROM test.college_survey_questions`
Martin Smith
I'm now getting --- Server: Msg 208, Level 16, State 1, Line 1Invalid object name 'SixthSenseUsers.college_survey_questions' but all the names look correct.
Scott Jackson
@Scott - I forgot to add a schema into your query. See my updated answer.
Martin Smith
+1  A: 

If you want the tables in sync continuously, you can use Transactional Replication.It will keep on copying the data from one table one db to another table of other DB.

Refer for further setup details over here:

http://www.databasejournal.com/features/mssql/article.php/1438201/Setting-Up-Transactional-Replication-A-Step-by-step-Guide.htm

Samiksha
A: 

Assuming no computed columns, Identity columns or FK constraints something like the following should work.

BEGIN TRAN
DELETE FROM SixthSenseUsers.dbo.college_survey_questions 

INSERT INTO SixthSenseUsers.dbo.college_survey_questions 

SELECT * FROM test.dbo.college_survey_questions

COMMIT
Martin Smith