Skip to content

Instantly share code, notes, and snippets.

@germ13
Created September 19, 2018 06:24
Show Gist options
  • Save germ13/6357fe23f00a93dbd45673c7a3a175ab to your computer and use it in GitHub Desktop.
Save germ13/6357fe23f00a93dbd45673c7a3a175ab to your computer and use it in GitHub Desktop.
Sync table across databases:
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
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