Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 help with update command
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

236 Posts

Posted - 03/14/2013 :  17:53:37  Show Profile  Reply with Quote
I have 2 tables

update vencidas SET [comentario] = a.comentario,
	                [Status] = a.Status,
			       (select ID, comentario, Status, ...
			        from vencidas
                                where ID=@GetRECID) a LEFT OUTER JOIN cobros ON A.ID = cobros.RECID

WHERE cobros.ACCOUNTNUM =(Select cobros2.ACCOUNTNUM FROM cobros2 WHERE cobros2.RECID = @GetRECID)
      AND Cobros.RECID <> @GetRECID

cobros                   vencidos      
-------                   ------
00015       354          354    .....
00015       275          275    bla bla
00015       112          765    .....

@GetRECID = 275

The update command should achieve that in vencidos those records are update, that do have a match for RECID = ID considering ONLY those records with ACCOUNTNUM 00015.

or in a discriptive way:
@GetRECID = 275
COBROS: go to the record where RECID = 275 -> ACCOUNTNUM = 00015
COBROS: Get all records where ACCOUNTNUM = 00015 -> take their RECID
VENCIDOS: Get all records with these found RECID that match ID
VENCIDOS: UPDATE these records with the content of the columns (comentario, status,..) for the record where ID = 275 = @GetRECID

The update would just affect one record:

--- ---------
354 bla bla

I really dont understand why my query doesn't find any records to update; I can't see the mistake, but no record is found to apply the update too.

My head is bursting,

Edited by - barnabeck on 03/14/2013 19:50:01

Flowing Fount of Yak Knowledge

6065 Posts

Posted - 03/14/2013 :  18:15:27  Show Profile  Reply with Quote
You are really over complicating this query. I think this is what you are looking for:

update v set

       v.[comentario] = c.comentario,
       v.[status] = c.[status]

from   (select accountNum from cobros where recid = @getRECID) d
inner join cobros c on c.accountNum = d.accountNum
inner join vencidas  v on v.[id] = c.recid

Be One with the Optimizer
Go to Top of Page

Posting Yak Master

236 Posts

Posted - 03/14/2013 :  19:36:56  Show Profile  Reply with Quote
It's not exactly what I was looking for, but I easily could adapt it to my needs. cobras does not have a column status, etc. The status is updated by the entry for status of record ID 275. So its:

v.[status] = (Select status from vencidos where id = @getRECID)

Thank for your help!
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 03/15/2013 :  11:28:42  Show Profile  Reply with Quote
do you realise that will update same values for all records in the update batch?

SQL Server MVP

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000