Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Do SQL update statements always end in WHERE clauses?

I’m developing a system such that users can input queries into the system and the system verifies the query. When the user enters an update statement, I wish to reuse that WHERE clause for an internal select statement.

For example, let’s say the user enters:

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

I’d like to reuse WHERE CustomerID = 1 (most likely by taking the substring) for a system select statement to verify that the recently updated rows are in a valid format:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

System enters:

SELECT * FROM Customers
WHERE CustomerID = 1;

Is this safe? Do all SQL statements end in WHERE clauses?

>Solution :

I’d check out the official docs for your specific flavor of SQL, as implementations vary. For instance,

  • MySQL allows for ORDER BY and LIMIT clauses after UPDATE…WHERE.
  • Microsoft SQL Server has an optional OPTION clause that can follow WHERE.
  • Oracle’s SQL looks like WHERE is safely the last part of an UPDATE (at least for the version linked here).
  • PostgreSQL allows for a RETURNING clause.
  • etc.

Of course, the WHERE clause could be omitted in any, though it’s not usually what you’d want to do.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading