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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 help with update command

Author  Topic 

barnabeck
Posting Yak Master

236 Posts

Posted - 2013-03-14 : 17:53:37
I have 2 tables

update vencidas SET [comentario] = a.comentario,
[Status] = a.Status,
...
from
(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
------- ------
ACCOUNTNUM RECID ID COMENTARIO
00015 354 354 .....
00015 275 275 bla bla
00015 112 765 .....


example:
@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:

VENCIDOS
ID COMENTARIO
--- ---------
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,
Thanks,
Martin

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-03-14 : 18:15:27
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
TG
Go to Top of Page

barnabeck
Posting Yak Master

236 Posts

Posted - 2013-03-14 : 19:36:56
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!
Martin
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

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

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -