Home > Database > Mysql Tutorial > How to Correctly Use INNER JOIN in SQL Server DELETE Statements?

How to Correctly Use INNER JOIN in SQL Server DELETE Statements?

Patricia Arquette
Release: 2025-01-19 06:08:10
Original
625 people have browsed it

How to Correctly Use INNER JOIN in SQL Server DELETE Statements?

SQL Server DELETE Statements with INNER JOIN: A Comprehensive Guide

Using INNER JOIN within SQL Server DELETE statements can sometimes lead to syntax errors. The most common error, "Incorrect syntax near the keyword 'INNER'," arises from an ambiguity in specifying the target table for deletion.

The solution is to use table aliases. This explicitly identifies the table from which rows should be removed. Here's a corrected example:

DELETE w
FROM WorkRecord2 w
INNER JOIN Employee e
  ON w.EmployeeRun = e.EmployeeNo
WHERE e.Company = '1' AND w.Date = '2013-05-06'
Copy after login

In this revised query, "w" serves as an alias for the WorkRecord2 table. This clearly designates the table affected by the DELETE operation, preventing the syntax error and ensuring the statement executes correctly. The WHERE clause then filters the rows to be deleted based on conditions involving both tables.

The above is the detailed content of How to Correctly Use INNER JOIN in SQL Server DELETE Statements?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template