简体   繁体   中英

Can I delete data from two tables by join condition in SQL Server 2008 R2?

This is my code.

DELETE tbemp.emp_id, 
        tbadd.emp_id 
FROM  TBEMPLOYEE tbemp 
INNER JOIN TBADDRESS tbadd ON (tbemp.emp_id=tbadd.emp_id)

it is not working. SQL Server 2008 R2 does not run it

Hope this should work, take a safe try,

DELETE TBEMPLOYEE FROM 
TBEMPLOYEE INNER JOIN 
TBADDRESS ON (TBEMPLOYEE.emp_id = TBADDRESS.emp_id)

You can also specify where condition here,

WHERE COL IN ('A','B',...)

You are using columns name(DELETE tbemp.emp_id, tbadd.emp_id) in place of table name and suggested by others , Delete works on a single table only.

Hope this work for you -

DELETE TBEMPLOYEE FROM 
TBEMPLOYEE INNER JOIN 
TBADDRESS ON (TBEMPLOYEE.emp_id = TBADDRESS.emp_id)

DELETE TBADDRESS FROM 
TBEMPLOYEE Right JOIN 
TBADDRESS ON (TBEMPLOYEE.emp_id = TBADDRESS.emp_id)

Execute in a same order.

declare @Tbl table ( emp_id int)

DELETE from tbemp
output deleted.emp_id into @t

FROM  TBEMPLOYEE tbemp 
JOIN TBADDRESS tbadd ON tbemp.emp_id=tbadd.emp_id

DELETE from tbadd

FROM TBADDRESS tbadd 
join @T T ON T.emp_id=tbadd.emp_id

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