DECLARE @startp int, @startr int,@alldatacount int, @tableA int,@tableAcount int, @tableB int,@tableBcount int
SELECT @startp=MAX(QID) FROM serverA.databaseA.dbo.tableA
SELECT @startr=MAX(QID) FROM dbo.tableA
if(@startp > @startr)
BEGIN
DECLARE @alldata TABLE (pQID int not null,TimeSS datetime not null,UserID varchar(16) not null,
AcctID varchar(16) not null,Symbol varchar(25) not null,Desk varchar(20) not null,Strategy varchar(20) not null,
CurrentNet bigint not null, TodayBought bigint not null,TodaySold bigint not null,
rQID int not null,rType varchar(10) not null,rAmount decimal(18,4) not null)
INSERT @alldata (
pQID,TimeSS,UserID,AcctID,Symbol,Desk,Strategy,CurrentNet,TodayBought,TodaySold,rQID,rType,rAmount)
SELECT
p1.QID,p1.TimeSS,p1.UserID,p1.AcctID,p1.Symbol,p1.Desk,p1.Strategy,p1.CurrentNet,p1.TodayBought,p1.TodaySold,
p2.QID,p2.Type,p2.Amount
FROM
serverA.databaseA.dbo.tableA p1 INNER JOIN serverA.databaseA.dbo.tableB p2
ON p1.QID = p2.QID AND p1.TimeSS >= CONVERT(CHAR(8),GETDATE(),112)
WHERE NOT EXISTS(SELECT 1 FROM dbo.tableA r1
WHERE p1.QID = r1.QID)
SET @alldatacount = @@ROWCOUNT
SELECT CONVERT(VARCHAR,@alldatacount) + ' Records copied to @alldata.'
IF(@alldatacount > 0)
BEGIN
SET IDENTITY_INSERT tableA ON
INSERT dbo.tableA(QID,TimeSS,UserID,AcctID,Symbol,Desk,Strategy,CurrentNet,TodayBought,TodaySold)
SELECT pQID,TimeSS,UserID,AcctID,Symbol,Desk,Strategy,CurrentNet,TodayBought,TodaySold
FROM @alldata
SET @tableAcount = @@ROWCOUNT
SET IDENTITY_INSERT tableA OFF
SELECT CONVERT(VARCHAR,@tableAcount) + ' Records copied to dbo.tableA from @alldata.'
IF(@tableAcount >0)
BEGIN
INSERT dbo.tableB(QID,Type,Amount)
SELECT rQID,rType,rAmount
FROM @alldata q1 INNER JOIN dbo.tableA q2 ON q1.rQID = q2.QID
AND q2.TimeSS >=CONVERT(CHAR(8),GETDATE(),112)
SET @tableBcount = @@ROWCOUNT
SELECT CONVERT(VARCHAR,@tableBcount) + ' Records copied to dbo.tableB from @alldata.'
END
IF(@@ERROR <> 0)
BEGIN
EXEC msdb..sp_send_dbmail @recipients= 'me',
@subject='blah blah',
@body='The synch job failed, please review.'
END
END
END
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
by: dbaSQLPosted on 2009-11-03 at 06:42:45ID: 25729367
the endresult of that table variable insert is the missing data from both tables, where the last three attributes in the variable are for tableB, the rest is for tableA
again, maybe the variable isn't the way to do this. it's simply a matter of copying from the two-table joined dataset from the primary server, into the same two tables on the redundancy, where not exists.