Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 466 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why can't I use an alias in a DELETE statement?

#1
In SQL Server Compact Edition in Visual Studio 2010 (maybe SQL Server and SQL in general, I don't know), this command works:

DELETE FROM foods WHERE (name IN ('chickens', 'rabbits'))

but this command produces an error of: `Error near identifier f. Expecting OUTPUT.`

DELETE FROM foods f WHERE (f.name IN ('chickens', 'rabbits'))

Reply

#2
The delete statement has strange syntax. It goes like this:

DELETE f FROM foods f WHERE (f.name IN ('chickens', 'rabbits'))
Reply

#3
To alias the table you'd have to say:

DELETE f FROM dbo.foods AS f WHERE f.name IN (...);

...though I fail to see the point of aliasing ***for this specific statement***, especially since (at least IIRC) this no longer conforms to strict ANSI, may cause unnecessary hurdles when writing for multiple platforms, and it introduces complexity and confusion for new users learning the basics of vanilla DML.

This will do and doesn't require an alias:

DELETE dbo.foods WHERE name IN (...);

But yes, as comments suggest, it may be necessary for other query forms (e.g. any DML combined with correlation, joins, `EXISTS`, etc). In SQL Server you can do this using, for example:

DELETE f
FROM dbo.foods AS f
INNER JOIN dbo.allergies AS a
ON f.FoodId = a.FoodId;

Just keep in mind this query may have to be constructed differently on {not SQL Server}.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through