Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does DELETE FROM ... FROM ... not error out?

Tags:

sql

sql-server

This bit within a stored proc is apparently valid sql:

DELETE TOP (@MaxRecords)
FROM Table
FROM Table B
INNER JOIN Table2 R ON B.fk = R.pk
WHERE R.Value < @DecVariable;

How can two FROM statements be put together and yet still be valid?

like image 673
user1729696 Avatar asked Dec 06 '25 03:12

user1729696


1 Answers

First of all TOP in delete syntax indicates that it is SQL Server.

It is perfect valid query, see DELETE:

FROM

An optional keyword that can be used between the DELETE keyword and the target table_or_view_name, or rowset_function_limited.

FROM table_source

Specifies an additional FROM clause. This Transact-SQL extension to DELETE allows specifying data from and deleting the corresponding rows from the table in the first FROM clause.

This extension, specifying a join, can be used instead of a subquery in the WHERE clause to identify rows to be removed.

DELETE: enter image description here

Object: enter image description here

like image 158
Lukasz Szozda Avatar answered Dec 07 '25 20:12

Lukasz Szozda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!