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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Mirror a Table

Author  Topic 

Steve2106
Posting Yak Master

183 Posts

Posted - 2013-02-04 : 06:17:16
Hi There,

I have 2 databases on the same server and I am in need of having an exact copy of the custormers table.

If a customer is added, updated or deleted in the customer table in database 1, I need it to be added, updated or deleted in the customer table in database 2

How would I go about doing that?

Thanks as always for your help, I appreciate your time.

Best Regards,


Steve

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-04 : 06:21:00
sounds like transactional replication to me

see

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

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-04 : 06:21:18
If a view is acceptable then the simplest thing is to create a view in the second databse (assuming of course, you are not going to update/delete/insert into the table in the second database)
USE YourDB2
GO
CREATE VIEW dbo.YourViewName
AS
SELECT col1,col2,col3 FROM YourDB1.dbo.YourTableName
GO
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-04 : 06:30:57
Keep in mind that view is a virtual copy and wont store data in it unless you index it.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-04 : 06:40:20
If I am not mistaken, indexed views cannot be created across databases (requires schemabinding, and cross database schemabinding not being loved etc.). If the data does need to be persisted, use transactional replication as Visakh suggested.
Go to Top of Page

Steve2106
Posting Yak Master

183 Posts

Posted - 2013-02-04 : 06:41:44
Hi Viskah,

Thanks for the reply.

I think this is a little beyond my access rights as it is a hosted server.
I thought there maybe a trigger or something that I could use.

Is there any other way to acheive what I need.

Thanks again for your help.

Best Regards,


Steve
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-04 : 06:45:54
trigger is an overkill for this,esp if amount of DML operations on the table is large

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -