SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

barnabeck
Posting Yak Master

Spain
194 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,
                                ...
				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

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

TG
Flowing Fount of Yak Knowledge

USA
6062 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
TG
Go to Top of Page

barnabeck
Posting Yak Master

Spain
194 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!
Martin
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 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
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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