Click here to Skip to main content
15,888,521 members
Home / Discussions / Database
   

Database

 
Questionmysql client mobile app Pin
Jassim Rahma30-Oct-11 4:58
Jassim Rahma30-Oct-11 4:58 
QuestionFill gaps in rates data with previous working day rates. Pin
Dev S30-Oct-11 3:00
Dev S30-Oct-11 3:00 
AnswerRe: Fill gaps in rates data with previous working day rates. Pin
Mycroft Holmes30-Oct-11 12:52
professionalMycroft Holmes30-Oct-11 12:52 
QuestionGrant a role to another role Pin
Danzy8329-Oct-11 12:31
Danzy8329-Oct-11 12:31 
QuestionJust installed SQL Server 2008 R2 - Where is the app to create/open/add/update/delete DB file? Pin
swampwiz28-Oct-11 0:40
swampwiz28-Oct-11 0:40 
AnswerRe: Just installed SQL Server 2008 R2 - Where is the app to create/open/add/update/delete DB file? Pin
Geoff Williams28-Oct-11 1:29
Geoff Williams28-Oct-11 1:29 
AnswerRe: Just installed SQL Server 2008 R2 - Where is the app to create/open/add/update/delete DB file? Pin
phil.o28-Oct-11 2:25
professionalphil.o28-Oct-11 2:25 
QuestionNeed help with update SQL with subquery Pin
Hypermommy26-Oct-11 3:14
Hypermommy26-Oct-11 3:14 
Hi all,

I'm trying to write an update statement with a subquery and can't quite get it right. Hoping you can help. Overall goal of the stored procedure I'm writing is to merge data and make sure I have no duplicates and that the responses associated with a potential duplicate are re-associated with the original record instead of the duplicate.

So......

Basic structure of tables is this:

Rosters table - contains all of the rosters in the database
RosterEntries table - contains a single record for each person in a roster. FK here is back to the rosters table.
ParticipantResponses - contains a record for each response a person gave. FK here is back to the RosterEntries table.

I am merging the rosters. So when I'm done, there will be one roster where there were two. During the merge, I need to make sure that no entries in the RosterEntries table is duplicated and also make sure that the responses that are tied to a duplicate record are reassociated with the original, not the duplicate.

I have an @Existing table variable. This is a subset of the RosterEntries table and it contains only those records that are associated with the original roster.

I have the @Dupes table variable. This is a subset of RosterEntries that contains only those records from the roster to be merged (the one that will be going away) which are duplicates of those already in the RosterEntries table.

So, let's say James Kirk is in the RosterEntries table twice, once associated with RosterID1 and once associated with RosterID2. RosterID2 is going away. So James Kirk's first record would be in my @Existing table variable. James Kirk's 2nd record will be in my @Dupes table and this is the one I want to get rid of. But before I do that, I have to associate his responses with that first record (the RosterID1 one) so I don't have orphaned responses. Basically, I'm trying to replace the FK in ParticipantResponses so that it points to Kirk's first record (RosterID1 record) not the 2nd one (RosterID2 record)

The SQL statement I've been working on is this:

SQL
update participantresponses pr, @dupes d 
set pr.rostEntID = 
(
	select e.recid 
	from @Existing e, @dupes d2
	where
			e.refid = d2.refid
		and	e.palias = d2.palias
		and e.palias2 = d2.palias2
		and e.pweight = d2.pweight
		and e.pHandicap = d2.phandicap
		and e.loginID = d2.loginid
		and e.password = d2.password
) 
where pr.rostEntID = d.recID


(I know... there's a lot of where clause activity in the subquery, but that's the specs... dupes are defined as those where all of those fields are equal. Same criteria was used to fill the @Dupes table)

The way I read this (obviously not quite right) is that I want to update the PR table, setting the PR.RostEntID = the recordID from the existing table where that existing entry matches the one in the dupes table.

The error I'm getting is:

Msg 102, Level 15, State 1, Line 125
Incorrect syntax near 'pr'.
Msg 156, Level 15, State 1, Line 139
Incorrect syntax near the keyword 'where'.


Could y'all help me figure out what I'm doing wrong and/or how I can get this replacement made? I know I could do it with a loop and that's probably what I'll end up doing for now but I just KNOW there has to be a way to batch process this.

Thanks in advance!
-- Denise

AnswerRe: Need help with update SQL with subquery Pin
Corporal Agarn26-Oct-11 3:46
professionalCorporal Agarn26-Oct-11 3:46 
AnswerRe: Need help with update SQL with subquery Pin
Hypermommy26-Oct-11 6:03
Hypermommy26-Oct-11 6:03 
QuestionSELECT ... GROUP BY Error Pin
MikeDhaan26-Oct-11 1:07
MikeDhaan26-Oct-11 1:07 
AnswerRe: SELECT ... GROUP BY Error Pin
Simon_Whale26-Oct-11 1:13
Simon_Whale26-Oct-11 1:13 
GeneralRe: SELECT ... GROUP BY Error Pin
MikeDhaan26-Oct-11 1:59
MikeDhaan26-Oct-11 1:59 
GeneralRe: SELECT ... GROUP BY Error Pin
Jörgen Andersson26-Oct-11 2:37
professionalJörgen Andersson26-Oct-11 2:37 
GeneralRe: SELECT ... GROUP BY Error Pin
MikeDhaan26-Oct-11 5:14
MikeDhaan26-Oct-11 5:14 
GeneralRe: SELECT ... GROUP BY Error Pin
Jörgen Andersson26-Oct-11 23:46
professionalJörgen Andersson26-Oct-11 23:46 
GeneralRe: SELECT ... GROUP BY Error Pin
Jörgen Andersson27-Oct-11 0:09
professionalJörgen Andersson27-Oct-11 0:09 
GeneralRe: SELECT ... GROUP BY Error Pin
Blue_Boy26-Oct-11 5:00
Blue_Boy26-Oct-11 5:00 
AnswerRe: SELECT ... GROUP BY Error Pin
Chris Meech26-Oct-11 2:45
Chris Meech26-Oct-11 2:45 
GeneralRe: SELECT ... GROUP BY Error Pin
MikeDhaan26-Oct-11 4:23
MikeDhaan26-Oct-11 4:23 
GeneralRe: SELECT ... GROUP BY Error Pin
Chris Meech26-Oct-11 4:27
Chris Meech26-Oct-11 4:27 
GeneralRe: SELECT ... GROUP BY Error Pin
MikeDhaan26-Oct-11 4:57
MikeDhaan26-Oct-11 4:57 
GeneralRe: SELECT ... GROUP BY Error Pin
Mycroft Holmes26-Oct-11 5:03
professionalMycroft Holmes26-Oct-11 5:03 
GeneralRe: SELECT ... GROUP BY Error Pin
Chris Meech26-Oct-11 5:14
Chris Meech26-Oct-11 5:14 
GeneralRe: SELECT ... GROUP BY Error Pin
MikeDhaan26-Oct-11 5:29
MikeDhaan26-Oct-11 5:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.