Created
September 19, 2018 06:24
-
-
Save germ13/6357fe23f00a93dbd45673c7a3a175ab to your computer and use it in GitHub Desktop.
Sync table across databases:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use SourceDB | |
go | |
drop table worker | |
create table [Worker]( | |
[ID] int not null PRIMARY KEY IDENTITY (1,1), | |
[FullName] varchar(30), | |
) | |
go | |
create trigger worker_sync_insert | |
on dbo.Worker | |
after insert | |
as | |
begin | |
set identity_insert TargetDb.dbo.Worker on; | |
insert into TargetDB.dbo.Worker ( | |
ID, | |
FullName ) | |
select ID, FullName from inserted; | |
end | |
go | |
create trigger worker_sync_update | |
on dbo.Worker | |
after update | |
as | |
begin | |
set identity_insert TargetDb.dbo.Worker on; | |
update TargetDB.dbo.Worker | |
set FullName = I.FullName | |
from inserted I | |
join TargetDB.dbo.Worker T | |
on T.id = I.id | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use TargetDB; | |
go | |
drop table SubscriptionWorker; | |
drop table Subscription; | |
drop table Subscriptioncategory; | |
drop table Worker; | |
--region | |
create table [Worker]( | |
[ID] int not null PRIMARY KEY IDENTITY (1,1), | |
[FullName] varchar(30), | |
) | |
create table SubscriptionCategory( | |
ID int not null primary key identity(1,1), | |
[name] varchar(24) not null | |
) | |
create table Subscription( | |
[ID] int not null primary key identity(1,1), | |
[Category] int not null foreign key references SubscriptionCategory(ID), | |
[Nombre] varchar(20) not null, | |
) | |
create table SubscriptionWorker( | |
[ID] int not null primary key identity(1,1), | |
[WorkerId] int not null foreign key references Worker(ID), | |
[SubscriptionId] int not null foreign key references Subscription(ID) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment