简体   繁体   中英

How to delete two records from two tables?

Hello I need to be able to search for a record that is a year old and then delete it. I have this script which allows me to delete the record from one table, based on a date given by another table,however I need to add code to this so that I am able to delete a record from a different table relating to CardID. The table that I need to delete from is table11 and Primary key is CardID.

I think I need a left join, but I am not to sure on how to go about it.

DECLARE @deleted TABLE (Card INT)

INSERT INTO @deleted
SELECT Card FROM table9
WHERE recordstatus = 4


DELETE table9
FROM @deleted d, table51

WHERE table51.ActionString LIKE '%' + CAST(d.card AS VARCHAR(20))+ '%'
AND table51.AuditDate <= (SELECT CONVERT(VARCHAR(8),today,112) FROM(SELECT DATEADD(YEAR,-1,GETDATE()) AS today)aa)
AND table09.Card = d.card

Thanks in advance, Hope you can help.

same as this question

edit: as @HLGEM mentioned, the WHERE clause goes where you expect it to go, after the join.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM