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
 Turning DELETE Into Update

Author  Topic 

BobRoberts
Posting Yak Master

109 Posts

Posted - 2009-06-24 : 09:50:07
I have this SQL statement, which deletes any row in tblTrans that has a column ItemId which does not match the item ID of tblItem, and which also has a tblTrans.ItemFlag which is a '1'. I'm trying to figure out how to turn it into an UPDATE statement in which, instead of deleting rows from tblTrans, it simply changes ItemFlag from '1' to '0'. Here is the statement:

DELETE tblTrans from tblItem i right join tblTrans u
on i.itemid = u.itemid where (i.ItemId IS NULL) AND (u.ItemFlag = '1')

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-06-24 : 09:53:49
update a
set a.ItemFlag = '1'
from tblTrans a
left join tblItem b on a.itemid = b.itemid
where b.itemid is null
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-24 : 09:53:59
UPDATE t
SET t.ItemFlag = 1
FROM tblTrans AS t
LEFT JOIN tblItem AS i ON i.ItemID = t.ItemID
WHERE i.ItemID IS NULL
AND t.ItemFlag = 0



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

BobRoberts
Posting Yak Master

109 Posts

Posted - 2009-06-24 : 10:08:29
quote:
Originally posted by Peso

UPDATE t
SET t.ItemFlag = 1
FROM tblTrans AS t
LEFT JOIN tblItem AS i ON i.ItemID = t.ItemID
WHERE i.ItemID IS NULL
AND t.ItemFlag = 0



E 12°55'05.63"
N 56°04'39.26"





What significance does the AS have? Is it necessary? Also, I'm trying to change the flag FROM 1 TO 0, but I think that is probably an oversight in your code.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-24 : 10:33:59
Probably.
UPDATE    t
SET t.ItemFlag = 0
FROM tblTrans AS t
LEFT JOIN tblItem AS i ON i.ItemID = t.ItemID
WHERE i.ItemID IS NULL
AND t.ItemFlag = 1



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-24 : 12:37:11
quote:
Originally posted by BobRoberts

quote:
Originally posted by Peso

UPDATE t
SET t.ItemFlag = 1
FROM tblTrans AS t
LEFT JOIN tblItem AS i ON i.ItemID = t.ItemID
WHERE i.ItemID IS NULL
AND t.ItemFlag = 0



E 12°55'05.63"
N 56°04'39.26"





What significance does the AS have? Is it necessary? Also, I'm trying to change the flag FROM 1 TO 0, but I think that is probably an oversight in your code.


AS is optional. You can just give alias alone also
Go to Top of Page
   

- Advertisement -