[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

problem with duplicate key violations

Asked by dbaSQL in SQL Server 2008, MS SQL Server

tableA = QID,TimeSS,UserID,AcctID,Symbol,Desk,Strategy,CurrentNet,TodayBought,TodaySold
tableB = QID,Type,Amount

QID is an IDENTITY, and the PK on tableA
the PK on tableB is QID,Type
The two tables relate based on QID  -- tableA.QID = tableB.QID

I am watching serverA.databaseA.dbo.tableA, copying all data from tableA and tableB into the same two tables on serverB where not exists.  

I've had it set up doing each table selectively, but there are recurrent problems where I'm not getting all data into serverB.  now i am working to change it.

basically, give me the MAX(QID) from both servers, databases, tables.  
@startp = MAX(QID) from primary server
@startr = MAX(QID) from redundancy server
IF @startp > @startr (it always will be), i need to copy all data from primary where not exists.  i attempted the below -- figured I'd write it all to a table variable (@alldata), insert @alldata based on a join between tableA and tableB on serverA, and then copy from said variable into both target tables on serverB.  

it is failing with duplicate constraint violation in serverB.databaseB.dbo.tableA.QID

fairly urgent, please advise
maybe the variable isn't the way to go.  pretty simple, i am copying tableA AND tableB from serverA to serverB every 10 minutes or so throughout the day, where not exists.
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:
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
[+][-]11/03/09 06:42 AM, ID: 25729367Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/03/09 07:48 AM, ID: 25730078Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: SQL Server 2008, MS SQL Server
Sign Up Now!
Solution Provided By: Sharath_123
Participating Experts: 1
Solution Grade: A
 
[+][-]11/03/09 08:06 AM, ID: 25730342Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/03/09 08:11 AM, ID: 25730400Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/03/09 08:17 AM, ID: 25730491Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20080625