I recently faced a delete syntax I was unaware of.
delete #fooTbl from #fooTbl where attr ='some'
The Official Microsoft documentation states:
DELETE FROM [database_name . [ schema ] . | schema. ] table_name
[ WHERE <search_condition> ]
[ OPTION ( <query_options> [ ,...n ] ) ]
[; ]
i have some perplexity about the command I wrote:
- What is and what is needed that parameter before the delete keyword (In my example coincides with table name)?
- What is the best practice?
Best Answer
Referencing the table is a possibility due to the fact that you could
DELETE FROM
with anINNER JOIN
.A simple example of deleting from
#footbl
with aninner join
to a different tableWhereas this generates an error due to not specifying what table to delete from
And this also works
In short, you need to specify the alias/table to delete from if you are referencing multiple tables, but you don't have to when you only reference the one.
In terms of best practice I don't think that it matters much for the single table. Aliasses can be useful as to make adapting / reusing scripts easier.
Some more examples in the Microsoft documentation
D. Using joins and subqueries to data in one table to delete rows in another table
Using the table name twice
Using an alias
Both serving the exact same purpose.