How to remove records using table joins in T-SQL?

I’m trying to remove some data from a table using a join in T-SQL but I’m running into problems. Here’s what I’ve got:

REMOVE 
FROM TimeSheet 
INNER JOIN Staff 
        ON StaffID=EmployeeID
WHERE Dept = 'Sales' 
    AND EntryDate = '2023-11-15'

When I run this, I get an error saying there’s something wrong with the INNER keyword. I’m using SQL Server 2008. Can anyone help me figure out how to make this work? I need to delete records from TimeSheet based on matching data in the Staff table. Thanks!

yo SwiftCoder15, zack’s onto smth there. DELETE is the way to go in T-SQL. Just a heads up tho, make sure u double-check ur WHERE clause before runnin it. don’t wanna accidentally wipe out more data than u meant to, ya know? been there, done that… not fun lol

You’ve received some good advice already, but I’d like to add a crucial point. When performing deletions with joins, it’s imperative to use table aliases. This not only improves readability but also prevents ambiguity. Here’s an optimized version of your query:

DELETE t
FROM TimeSheet t
INNER JOIN Staff s ON t.StaffID = s.EmployeeID
WHERE s.Dept = ‘Sales’
AND t.EntryDate = ‘2023-11-15’

This structure clearly specifies which table you’re deleting from and how it relates to the Staff table. Always test such queries on a backup or development environment first to ensure they behave as expected.

hey there! have you tried using DELETE instead of REMOVE? that might be the issue. also, you could try putting the table you want to delete from after DELETE, like this:

DELETE TimeSheet
FROM TimeSheet
INNER JOIN Staff ON StaffID=EmployeeID
WHERE…

curious to hear if that works for you? let us know!